2010_S2_Jackson_Mathew_HH_1

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 Sub btnSaveText_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.

 Dim inputString As String  = txtSaveText.Text <span style="border-collapse: collapse; color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;"> Dim <span style="border-collapse: collapse; font-family: 'Lucida Console',Monaco,monospace;"> dirString <span style="border-collapse: collapse; color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;">As String = CurDir

<span style="border-collapse: collapse; font-family: Arial,Helvetica,sans-serif;">
 * <span style="border-collapse: collapse; font-family: Arial,Helvetica,sans-serif;">Now for the code that actually saves the file.

<span style="color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;">Dim <span style="font-family: 'Lucida Console',Monaco,monospace;"> file <span style="color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;">As <span style="font-family: 'Lucida Console',Monaco,monospace;"> System.IO. <span style="color: #008080; font-family: 'Lucida Console',Monaco,monospace;">FileStream <span style="font-family: 'Lucida Console',Monaco,monospace;"> <span style="font-family: 'Lucida Console',Monaco,monospace;">file = System.IO. <span style="color: #008080; font-family: 'Lucida Console',Monaco,monospace;">File <span style="font-family: 'Lucida Console',Monaco,monospace;">.Create(dirString & <span style="color: #800000; font-family: 'Lucida Console',Monaco,monospace;">"test.txt" <span style="font-family: 'Lucida Console',Monaco,monospace;">) <span style="font-family: 'Lucida Console',Monaco,monospace;">file.Close <span style="color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;">My <span style="font-family: 'Lucida Console',Monaco,monospace;">.Computer.FileSystem.WriteAllText(dirString, inputString, <span style="color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;">True <span style="font-family: 'Lucida Console',Monaco,monospace;">)


 * <span style="font-family: Arial,Helvetica,sans-serif;">**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.

<span style="color: #008080; font-family: 'Lucida Console',Monaco,monospace;">MessageBox <span style="font-family: 'Lucida Console',Monaco,monospace;">.Show( <span style="color: #800000; font-family: 'Lucida Console',Monaco,monospace;">"Text file has been created as " <span style="font-family: 'Lucida Console',Monaco,monospace;"> & dirString, <span style="color: #800000; font-family: 'Lucida Console',Monaco,monospace;">"Config Created" <span style="font-family: 'Lucida Console',Monaco,monospace;">, <span style="color: #008080; font-family: 'Lucida Console',Monaco,monospace;">MessageBoxButtons <span style="font-family: 'Lucida Console',Monaco,monospace;">.OK, <span style="color: #008080; font-family: 'Lucida Console',Monaco,monospace;">MessageBoxIcon <span style="font-family: 'Lucida Console',Monaco,monospace;">.Exclamation)


 * <span style="font-family: Arial,Helvetica,sans-serif;">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.

<span style="border-collapse: collapse; color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;">Private Sub <span style="border-collapse: collapse; font-family: 'Lucida Console',Monaco,monospace;">btnSaveText_Click( <span style="border-collapse: collapse; color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;">ByVal <span style="border-collapse: collapse; font-family: 'Lucida Console',Monaco,monospace;">sender <span style="border-collapse: collapse; color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;">As <span style="border-collapse: collapse; font-family: 'Lucida Console',Monaco,monospace;">System.Object, ByVal e As <span style="border-collapse: collapse; font-family: 'Lucida Console',Monaco,monospace;">System . <span style="border-collapse: collapse; color: #008080; font-family: 'Lucida Console',Monaco,monospace;">EventArgs ) <span style="border-collapse: collapse; color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;">Handles <span style="border-collapse: collapse; font-family: 'Lucida Console',Monaco,monospace;">btnSaveText.Click <span style="border-collapse: collapse; color: #0000ff; font-family: 'Lucida Console',Monaco,monospace; margin: 0px; padding: 0px;"> Dim <span style="font-family: 'Lucida Console',Monaco,monospace; padding-top: 0px;">inputString <span style="color: #0000ff; font-family: 'Lucida Console',Monaco,monospace; padding-top: 0px;">As String <span style="font-family: 'Lucida Console',Monaco,monospace; margin-bottom: 0px; margin-top: 0px; padding-bottom: 0px; padding-top: 0px;"> = txtSaveText.Text <span style="font-family: Arial,Helvetica,sans-serif; font-style: normal; font-weight: normal; margin: 0px; padding: 0px;"><span style="border-collapse: collapse; color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;"> Dim <span style="border-collapse: collapse; font-family: 'Lucida Console',Monaco,monospace;"> dirString <span style="border-collapse: collapse; color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;">As String = <span style="border-collapse: collapse; font-family: 'Lucida Console',Monaco,monospace;">CurDir <span style="color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;">Dim <span style="font-family: 'Lucida Console',Monaco,monospace;"> file <span style="color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;">As <span style="font-family: 'Lucida Console',Monaco,monospace;">System.IO .<span style="color: #008080; font-family: 'Lucida Console',Monaco,monospace;">FileStream <span style="font-family: 'Lucida Console',Monaco,monospace;"> <span style="font-family: 'Lucida Console',Monaco,monospace;"> file = System.IO. File<span style="font-family: 'Lucida Console',Monaco,monospace;">.Create(dirString & <span style="color: #800000; font-family: 'Lucida Console',Monaco,monospace;">"test.txt" <span style="font-family: 'Lucida Console',Monaco,monospace;">) <span style="font-family: 'Lucida Console',Monaco,monospace;"> file.Close <span style="color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;"> My <span style="font-family: 'Lucida Console',Monaco,monospace;">.Computer.FileSystem.WriteAllText(dirString, inputString, <span style="color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;">True <span style="font-family: 'Lucida Console',Monaco,monospace;">) <span style="color: #008080; font-family: 'Lucida Console',Monaco,monospace;"> MessageBox <span style="font-family: 'Lucida Console',Monaco,monospace;">.Show( <span style="color: #800000; font-family: 'Lucida Console',Monaco,monospace;">"Text file has been created as " <span style="font-family: 'Lucida Console',Monaco,monospace;"> & dirString, "Config Created", <span style="color: #008080; font-family: 'Lucida Console',Monaco,monospace;">MessageBoxButtons <span style="font-family: 'Lucida Console',Monaco,monospace;">.OK, <span style="color: #008080; font-family: 'Lucida Console',Monaco,monospace;"> MessageBoxIcon <span style="font-family: 'Lucida Console',Monaco,monospace;">.Exclamation) <span style="border-collapse: collapse; color: #0000ff; font-family: 'Lucida Console',Monaco,monospace;">End Sub