where do i place the following code?

synchro

New member
Joined
Nov 8, 2005
Messages
2
Programming Experience
Beginner
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=&quot] [/FONT]Start a new Project and Open its properties (Right-click project name in solution explorer).
2.[FONT=&quot] [/FONT]In the Imports Section type System.IO in the Namespace box and click Add Import.

C:%5CDOCUME%7E1%5C102004%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_image002.jpg


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

C:%5CDOCUME%7E1%5C102004%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_image004.jpg



6.[FONT=&quot] [/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=&quot] [/FONT]Now let’s create the subroutines to write to a text file. Enter the following code in the usual place:

==============================================
[FONT=&quot]Sub[/FONT][FONT=&quot] WriteFile()[/FONT]
[FONT=&quot] Dim sw As StreamWriter[/FONT]
[FONT=&quot] Try[/FONT]
[FONT=&quot]sw = New [/FONT]
[FONT=&quot]StreamWriter(File.Open("a:\logfile.txt", _ FileMode.OpenOrCreate))[/FONT]
[FONT=&quot] Try[/FONT]
[FONT=&quot] sw.BaseStream.Seek(0, SeekOrigin.End)[/FONT]
[FONT=&quot] sw.WriteLine("Log entry added at: " & _[/FONT]
[FONT=&quot] Now.ToShortTimeString)[/FONT]
[FONT=&quot] sw.Flush()[/FONT]
[FONT=&quot] Finally[/FONT]
[FONT=&quot] sw.Close()[/FONT]
[FONT=&quot] End Try[/FONT]
[FONT=&quot] Catch exc As Exception[/FONT]
[FONT=&quot] End Try[/FONT]
[FONT=&quot] End Sub[/FONT]
[FONT=&quot] [/FONT]
================================================

8.[FONT=&quot] [/FONT]Now let’s create the subroutine to read from the text file. Enter the following code in the usual spot:
================================================

[FONT=&quot] Sub ReadFile()[/FONT]
[FONT=&quot] Dim sr As StreamReader[/FONT]
[FONT=&quot] Dim s As String[/FONT]
[FONT=&quot] Try[/FONT]
[FONT=&quot] sr = New StreamReader(File.Open("a:\logfile.txt", FileMode.Open))[/FONT]
[FONT=&quot] Try[/FONT]
[FONT=&quot] TextBox1.Text = sr.ReadToEnd[/FONT]
[FONT=&quot] Finally[/FONT]
[FONT=&quot] sr.Close()[/FONT]
[FONT=&quot] End Try[/FONT]
[FONT=&quot] Catch exc As Exception[/FONT]
[FONT=&quot] TextBox1.Text = exc.Message[/FONT]
[FONT=&quot] End Try[/FONT]
[FONT=&quot] End Sub[/FONT]
[FONT=&quot] [/FONT]
==================================================

9.[FONT=&quot] [/FONT]Run your application and press the button multiple times.
 
Back
Top