Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

I hope you can help; I�m not a very experienced .net coder!

 

I'm trying to create a standard text file within a directory donated by a variable. How do I do this as it only seems to accept a filename?

 

Here is my current code:

 

Public Shared Sub Main()

           ' Create a reference to a file.
           Dim fi As New FileInfo("temp.txt")
           ' Actually create the file.
           Dim fs As FileStream = fi.Create()

 

I have a variable called pdfloc which has a directory location in it such as C:\Data\ for example.

 

I want to put temp.txt within this variable location. I have tried the following, however I know it's not correct:-

 

           Dim fi As New FileInfo(pdfloc, "temp.txt")

 

Many thanks for your help. :)

Posted

This shows in the error list, highlighting the pdfloc variable:-

 

Reference to a non-shared member requires an object reference.

 

Thanks for your help.

Posted

Sorry, here we go:

 

Imports System
Imports System.IO
Imports System.Collections

Public Class StartForm

   ' Create var for location
   Dim pdfloc As String
   Private Sub StartForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       MsgBox("On the next window select the folder containing the PDF files.")

       FolderBrowserDialog1.ShowDialog()
       If FolderBrowserDialog1.SelectedPath = FolderBrowserDialog1.SelectedPath Then
           PdfLocationLbl.Text = FolderBrowserDialog1.SelectedPath
           pdfloc = FolderBrowserDialog1.SelectedPath

       End If
   End Sub

   Private Sub createmetadatabtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles createmetadatabtn.Click
       DeleteTest.Main()
   End Sub

   Public Class DeleteTest


       Public Shared Sub Main()

           ' Create a reference to a file.
           Dim fi As New FileInfo(pdfloc & "temp.txt")
           ' Actually create the file.
           Dim fs As FileStream = fi.Create()
           ' Close the file.
           fs.Close()
       End Sub 'Main

   End Class 'DeleteTest

End Class

  • Administrators
Posted

pdfloc is declared within the StartForm class and is therefore not available from the DeleteTest class, you could either make the variable public (a bad idea), or create a read only property that returns the value of pdfloc.

 

Also is there a particular reason the sub is called Main? That is normally reserved for the applications entry point. You may want to create a parameter in the Main method though and pass pdfloc that way.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...