carbcycle Posted May 9, 2005 Posted May 9, 2005 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. :) Quote
Administrators PlausiblyDamp Posted May 9, 2005 Administrators Posted May 9, 2005 Dim fi As New FileInfo(pdfloc & "temp.txt") Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
carbcycle Posted May 9, 2005 Author Posted May 9, 2005 This shows in the error list, highlighting the pdfloc variable:- Reference to a non-shared member requires an object reference. Thanks for your help. Quote
Administrators PlausiblyDamp Posted May 9, 2005 Administrators Posted May 9, 2005 Could you post more of your code? Cannot really tell where the problem lies from just one line. Where is the above code being executed from and where is pdfloc defined? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
carbcycle Posted May 9, 2005 Author Posted May 9, 2005 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 Quote
Administrators PlausiblyDamp Posted May 10, 2005 Administrators Posted May 10, 2005 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.