Saving a simple text file to the windows file system using a visual basic program
As I studied Visual Basic this semester I was making a program to write a configuration file to the same directory as the visual basic executable.
I found this a very useful feature, and would like to show you the basics of saving a text file in the same directory as the visual basic executable of your program.
This could be used for saving log files, or for what I used it for - writing a configuration file for another piece of software which made you write them manually.
On a blank form create a button called btnSaveText and a text box named txtSaveText
Create a click event for btnSaveText
Private SubbtnSaveText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveText.Click
End Sub
Create a variable for the text that is inside txtSaveText and for the Current Directory of the visual basic executable.
DiminputString As String = txtSaveText.Text Dim dirString As String = CurDir()
Now for the code that actually saves the file.
Dim file As System.IO.FileStream file = System.IO.File.Create(dirString & "test.txt") file.Close() My.Computer.FileSystem.WriteAllText(dirString, inputString, True)
NOTE! This may throw up an UnauthorizedAccessException error when you test the program, this means that the user running the program does not have the user access rights to save the text file to the directory that it's meant to be saving in. You can google running visual basic in elevated user mode, or check the Visual Studio help files for it - I believe you can compile the program so that it runs in elevated mode at run time - however I'm not covering that in this Handy Hint!
I think we should have a MessageBox pop up and tell the user where the text file has been saved, we do that by using the dirString variable in a MessageBox like so.
MessageBox.Show("Text file has been created as " & dirString, "Config Created", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
This is what my code looks like after it's done - I get the UnauthorizedAccessException when I hit the save button, simply changing the dirString variable from CurDir() to "C:\" or something should fix this - or do some exception handling - you could even have input to change the directory that the text file saves in.
Private SubbtnSaveText_Click(ByValsenderAsSystem.Object, ByVal e AsSystem.EventArgs) HandlesbtnSaveText.Click DiminputStringAs String = txtSaveText.Text Dim dirString As String = CurDir() Dim file AsSystem.IO.FileStream file = System.IO.File.Create(dirString & "test.txt") file.Close() My.Computer.FileSystem.WriteAllText(dirString, inputString, True) MessageBox.Show("Text file has been created as " & dirString, "Config Created",MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End Sub
Saving a simple text file to the windows file system using a visual basic program
As I studied Visual Basic this semester I was making a program to write a configuration file to the same directory as the visual basic executable.
I found this a very useful feature, and would like to show you the basics of saving a text file in the same directory as the visual basic executable of your program.
This could be used for saving log files, or for what I used it for - writing a configuration file for another piece of software which made you write them manually.
Private Sub btnSaveText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveText.Click
End Sub
Dim inputString As String = txtSaveText.Text
Dim dirString As String = CurDir()
Dim file As System.IO.FileStream
file = System.IO.File.Create(dirString & "test.txt")
file.Close()
My.Computer.FileSystem.WriteAllText(dirString, inputString, True)
MessageBox.Show("Text file has been created as " & dirString, "Config Created", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Private Sub btnSaveText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveText.Click
Dim inputString As String = txtSaveText.Text
Dim dirString As String = CurDir()
Dim file As System.IO.FileStream
file = System.IO.File.Create(dirString & "test.txt")
file.Close()
My.Computer.FileSystem.WriteAllText(dirString, inputString, True)
MessageBox.Show("Text file has been created as " & dirString, "Config Created", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Sub