Open a text file from my form

John Cassell

Well-known member
Joined
Mar 20, 2007
Messages
65
Programming Experience
Beginner
Hi There,

I have just added a new text file to my project (AppHistory.txt) and I would like to display it when a user clicks on a particular button. I can't seem to find out how to get this to open as it doesn't seem to actually exist anywhere.

Can someone point out my mistake please?

Thanks

John
 
This question has nothing to do with Windows Forms. Reading files is an I/O issue. Moved.

Select your text file and check its properties to make sure it's being copied to the output directory. The output directory is the same directory your EXE is created in. In code you can get that folder path with Application.StartupPath, so you can get to your text file from there.
 
You can also add the file to project settings and access it by binding a control to the setting or in code with My.Settings.
 
Hi Fellas, thanks for the reply and apologies for the wrong forum.

My txt file properties say 'Copy To Output - Copy Always'. I don't really know where to start with binding a control to the setting. Could you advise?

Thanks again

John
 
You don't have to bind the setting to a control. Look up "application settings" topic in help files, they are very easy to use. Actually what I meant was "application resources", which you similarily access afterwards with My.Resources in code, the resources don't bind to controls.
 
Hi Thanks a lot for that. I can now see the text file in My.Resources.AppHistory

Even though this seems like it should be one of the simplest things, I still can't get the file to actually open.

I have tried FileOpen, Resources.ResourceReader and a load of others but nothing seems to work. Can someone help with how to get this file to open?

Thanks

John
 
It is no longer a file when you have added it to resources, just a resource.
VB.NET:
Dim text As String = My.Resources.AppHistory
You can however write the resource to a file:
VB.NET:
My.Computer.FileSystem.WriteAllText("filename.txt", My.Resources.AppHistory, False)
For read/write access to text use an application setting with User scope.
 
Thanks John,

sorry to be a pain but I really don't know how to say 'Show the text in this document to the user'

I tried both what you wrote there and although it gives no error, it doesn't show anything either...

VB.NET:
Private Sub ProgramHistoryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgramHistoryToolStripMenuItem.Click
        Dim text As String = My.Resources.AppHistory
        My.Computer.FileSystem.WriteAllText("AppHistory.txt", My.Resources.AppHistory, False)
    End Sub
 
Perhaps you can use a message box?
VB.NET:
MessageBox.Show(My.Resources.AppHistory)
Or display it in a Textbox ?
VB.NET:
Textbox1.Text = My.Resources.AppHistory
 
Back
Top