my lectuer recently put up the following tutorial:
but he assumed we know where to put each piece of code
im gonna copy and past the tutorial now:
thanks guys!
Reading and Writing to a File
1.[FONT="] [/FONT]Start a new Project and Open its properties (Right-click project name in solution explorer).
2.[FONT="] [/FONT]In the Imports Section type System.IO in the Namespace box and click Add Import.
3.[FONT="] [/FONT]Add a TextBox and a Command Button to the Form.
4.[FONT="] [/FONT]Set the multiline property of the text to True.
5.[FONT="] [/FONT]Label the button : “Write and Show File”
6.[FONT="] [/FONT]Double click the button and add the following code to the click event:
=============================================
Call WriteFile()
Call ReadFile ()
=============================================
Before we start using Random access files, we are going to use the Microsoft StreamWriter class. It is the easiest way to write a stream characters. In this example we are going to grab the system time and add it to a text file. This would be useful if you wanted to create a log file for your application. In this example I am saving to the floppy drive. Make sure to change your file path to an appropriate location.
7.[FONT="] [/FONT]Now let’s create the subroutines to write to a text file. Enter the following code in the usual place:
==============================================
[FONT="]Sub[/FONT][FONT="] WriteFile()[/FONT]
[FONT="] Dim sw As StreamWriter[/FONT]
[FONT="] Try[/FONT]
[FONT="]sw = New [/FONT]
[FONT="]StreamWriter(File.Open("a:\logfile.txt", _ FileMode.OpenOrCreate))[/FONT]
[FONT="] Try[/FONT]
[FONT="] sw.BaseStream.Seek(0, SeekOrigin.End)[/FONT]
[FONT="] sw.WriteLine("Log entry added at: " & _[/FONT]
[FONT="] Now.ToShortTimeString)[/FONT]
[FONT="] sw.Flush()[/FONT]
[FONT="] Finally[/FONT]
[FONT="] sw.Close()[/FONT]
[FONT="] End Try[/FONT]
[FONT="] Catch exc As Exception[/FONT]
[FONT="] End Try[/FONT]
[FONT="] End Sub[/FONT]
[FONT="] [/FONT]
================================================
8.[FONT="] [/FONT]Now let’s create the subroutine to read from the text file. Enter the following code in the usual spot:
================================================
[FONT="] Sub ReadFile()[/FONT]
[FONT="] Dim sr As StreamReader[/FONT]
[FONT="] Dim s As String[/FONT]
[FONT="] Try[/FONT]
[FONT="] sr = New StreamReader(File.Open("a:\logfile.txt", FileMode.Open))[/FONT]
[FONT="] Try[/FONT]
[FONT="] TextBox1.Text = sr.ReadToEnd[/FONT]
[FONT="] Finally[/FONT]
[FONT="] sr.Close()[/FONT]
[FONT="] End Try[/FONT]
[FONT="] Catch exc As Exception[/FONT]
[FONT="] TextBox1.Text = exc.Message[/FONT]
[FONT="] End Try[/FONT]
[FONT="] End Sub[/FONT]
[FONT="] [/FONT]
==================================================
9.[FONT="] [/FONT]Run your application and press the button multiple times.
but he assumed we know where to put each piece of code
im gonna copy and past the tutorial now:
thanks guys!
Reading and Writing to a File
1.[FONT="] [/FONT]Start a new Project and Open its properties (Right-click project name in solution explorer).
2.[FONT="] [/FONT]In the Imports Section type System.IO in the Namespace box and click Add Import.

3.[FONT="] [/FONT]Add a TextBox and a Command Button to the Form.
4.[FONT="] [/FONT]Set the multiline property of the text to True.
5.[FONT="] [/FONT]Label the button : “Write and Show File”

6.[FONT="] [/FONT]Double click the button and add the following code to the click event:
=============================================
Call WriteFile()
Call ReadFile ()
=============================================
Before we start using Random access files, we are going to use the Microsoft StreamWriter class. It is the easiest way to write a stream characters. In this example we are going to grab the system time and add it to a text file. This would be useful if you wanted to create a log file for your application. In this example I am saving to the floppy drive. Make sure to change your file path to an appropriate location.
7.[FONT="] [/FONT]Now let’s create the subroutines to write to a text file. Enter the following code in the usual place:
==============================================
[FONT="]Sub[/FONT][FONT="] WriteFile()[/FONT]
[FONT="] Dim sw As StreamWriter[/FONT]
[FONT="] Try[/FONT]
[FONT="]sw = New [/FONT]
[FONT="]StreamWriter(File.Open("a:\logfile.txt", _ FileMode.OpenOrCreate))[/FONT]
[FONT="] Try[/FONT]
[FONT="] sw.BaseStream.Seek(0, SeekOrigin.End)[/FONT]
[FONT="] sw.WriteLine("Log entry added at: " & _[/FONT]
[FONT="] Now.ToShortTimeString)[/FONT]
[FONT="] sw.Flush()[/FONT]
[FONT="] Finally[/FONT]
[FONT="] sw.Close()[/FONT]
[FONT="] End Try[/FONT]
[FONT="] Catch exc As Exception[/FONT]
[FONT="] End Try[/FONT]
[FONT="] End Sub[/FONT]
[FONT="] [/FONT]
================================================
8.[FONT="] [/FONT]Now let’s create the subroutine to read from the text file. Enter the following code in the usual spot:
================================================
[FONT="] Sub ReadFile()[/FONT]
[FONT="] Dim sr As StreamReader[/FONT]
[FONT="] Dim s As String[/FONT]
[FONT="] Try[/FONT]
[FONT="] sr = New StreamReader(File.Open("a:\logfile.txt", FileMode.Open))[/FONT]
[FONT="] Try[/FONT]
[FONT="] TextBox1.Text = sr.ReadToEnd[/FONT]
[FONT="] Finally[/FONT]
[FONT="] sr.Close()[/FONT]
[FONT="] End Try[/FONT]
[FONT="] Catch exc As Exception[/FONT]
[FONT="] TextBox1.Text = exc.Message[/FONT]
[FONT="] End Try[/FONT]
[FONT="] End Sub[/FONT]
[FONT="] [/FONT]
==================================================
9.[FONT="] [/FONT]Run your application and press the button multiple times.