0% found this document useful (0 votes)
66 views

Cert IV Multimedia/Websites Programming 2 File Handling and System - IO

The document provides instructions for a series of activities to demonstrate file handling and reading/writing to files using System.IO classes in Visual Basic .NET. The activities include: [1] Creating a text file using IO.File.Create; [2] Writing content to a file using IO.StreamWriter; [3] Reading a single line from a file using IO.StreamReader; [4] Loading the entire contents of a text file using IO.StreamReader; [5] Moving a file using IO.File.Move; [6] Copying a file using IO.File.Copy; and [7] Deleting a file using IO.File.Delete. The final activity demonstrates logging file open/save

Uploaded by

api-27070090
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Cert IV Multimedia/Websites Programming 2 File Handling and System - IO

The document provides instructions for a series of activities to demonstrate file handling and reading/writing to files using System.IO classes in Visual Basic .NET. The activities include: [1] Creating a text file using IO.File.Create; [2] Writing content to a file using IO.StreamWriter; [3] Reading a single line from a file using IO.StreamReader; [4] Loading the entire contents of a text file using IO.StreamReader; [5] Moving a file using IO.File.Move; [6] Copying a file using IO.File.Copy; and [7] Deleting a file using IO.File.Delete. The final activity demonstrates logging file open/save

Uploaded by

api-27070090
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Cert IV Multimedia/Websites

Programming 2
File Handling and System.IO

Assessment Item: File Handling and System.IO

Assessment Weight : 10%


Due Date: October 20, 2009

Instructions:
Read & complete the following activities. Your project files are to be submitted on a disk
clearly labeled with your name, student number and the name of this assignment ‘Assessment:
File Handling and System.IO’.

System.IO is a Collection of classes that allow reading and writing to text files and data streams,
and types that provide basic file and directory support.

Including the following classes:

IO.File IO.StreamReader IO.StreamWriter

Provides methods for working Implements a text reader Implements a text writer
with text files such as:

- creation
- deletion
- moving
- copying
- opening

The following activities will demonstrate some basic methods.

Create the interface.

• Create a new Windows project called FileHandling.


• Rename Form1 to frmFileHandling and give it a text value of “File Handling”.
• Place the following controls on the interface:

Control: Properties:
Name: btnCreateFile Text: Create File

Name: btnWriteFile Text: Write File

Name: btnReadLine Text: Read Line

Name: btnReadFile Text: Read File

Name: btnMoveFile Text: Move File

Name: btnCopyFile Text: Copy File

Name: btnDeleteFile Text: btnDeleteFile

1
Cert IV Multimedia/Websites
Programming 2
File Handling and System.IO

Name: txtFileContents Multiline: True


Scrollbars: Vertical

Activity 1:
Use IO.File.Create to create a text file.

IO.File.Create(destination)

• Double-click btnCreateFile and enter the following code in the click event:

IO.File.Create("c:\MyCreateFile.txt")

• Run the project, click btnCreateFile


• Stop the project.
• Navigate to c:\ drive to discover the newly created file MyCreateFile.txt.

Activity 2:
Use IO.StreamWriter to create a text file with content.

• Double-click btnWriteFile and enter the following code in the click event:

Dim objWriter As New IO.StreamWriter("c:\MyWriteFile.txt")


objWriter.WriteLine("I am a Multimedia RockStar!!!")
objWriter.Close()

• Run the project, click btnWriteFile


• Stop the project.
• Navigate to c:\ drive to discover the newly created file MyWriteFile.txt
• Open the file and read the text.

Activity 3:
Use IO.StreamReader to read a line of text from file.

• Double-click btnReadLine and enter the following code in the click event:

Dim objReader As IO.StreamReader


objReader = IO.File.OpenText("c:\ MyWriteFile.txt")
Me.txtFileContents.Text = objReader.ReadLine
objReader.Close()

• Run the project, click btnReadLine

2
Cert IV Multimedia/Websites
Programming 2
File Handling and System.IO

Activity 4:
Use IO.StreamReader to load entire text file contents.

• Open MyWriteFile.txt and add a couple of paragraphs text to the existing text.
• Save and close the file.
• Double-click btnReadFile and enter the following code in the click event:

Dim objReader As IO.StreamReader


objReader = IO.File.OpenText("c:\ MyWriteFile.txt")
Me.txtFileContents.Text = objReader.ReadToEnd
objReader.Close()

• Run the project, click btnReadFile

Activity 5:
Use IO.File.Move to move a text file.

IO.File.Move(Source, destination)

• Create a new folder on c:\ drive called MyTextFiles.


• Double-click btnMoveFile and enter the following code in the click event:

IO.File.Move(("c:\MyCreateFile.txt"), _
("c:\MyTextFiles\ MyCreateFile.txt"))

• Run the project, click btnMoveFile


• Navigate to the folder MyTextFiles on c:\ drive to discover the moved file.

Activity 6:
Use IO.File.Copy to copy a text file.

IO.File.Copy(Source, destination)

• Double-click btnCopyFile and enter the following code in the click event:

IO.File.Copy(("c:\MyTextFiles\ MyCreateFile.txt"), _
("c:\ MyCreateFile.txt"))

• Run the project, click btnCopyFile


• Navigate to c:\ drive to discover the copied file.

3
Cert IV Multimedia/Websites
Programming 2
File Handling and System.IO

Activity 7:
Use IO.File.Delete to delete a text file.

IO.File.Delete(PathName)

• Double-click btnDeleteFile and enter the following code in the click event:

IO.File.Delete("c:\MyTextFiles\ MyCreateFile.txt")

• Run the project, click btnDeleteFile


• Navigate to c:\ drive to confirm the file has been deleted.
• Save and close the FileHandling Windows project.

File Handling and process logging

As programs and development teams grow in size it becomes necessary to build in processes
which document system activities.

The following activity will build a small text editor program. This program will also write log entries
to a text file each time a file is opened or saved.

Activity 8:
Text editor and log files.

• Create a new Windows project called TextEditor.


• Rename Form1 to frmTextEditor with a text value of “Text Editor”.
• Place the following controls on the interface:

Control: Properties:
Name: btnOpenFile Text: Open File

Name: btnSaveFileAs Text: Save File As

Name: btnOpenLog Text: Open Log

Name: btnSaveLog Text: Save Log

Name: ofdOpenFile FileName: Empty

Name: sfdSaveFile FileName: Empty

Name: btnDeleteFile Text: btnDeleteFile

Name: txtEditor Multiline: True


Scrollbars: Vertical

4
Cert IV Multimedia/Websites
Programming 2
File Handling and System.IO

Write file log procedure.

Next we will create the procedure that will write to the log file.

• Switch to code view and enter the following code to create the WriteFileLog procedure:

Private Sub WriteFileLog(ByVal Action As String, ByVal FileName As


String)

End Sub

This procedure requires that two arguments (the parameters in the parentheses) be passed to it
by the calling procedure:

Argument 1: Action is a string containing a phrase describing the action the program is taking.
Argument 2: FileName is a string containing the name of the file being opened or saved.

The WriteFileLog will test two conditions. The first condition is whether the specified log file
exists. If it does exist, the AppendText method will be called to add the new text to the existing
file. If it does not exists, the CreateText method will be used to create the file before writing the
new text.

• Enter the following lines of code into the WriteFileLog procedure:


• Check that the file we want to write to exists using the Exists method.

If IO.File.Exists("c:/TextEditorLog.txt") Then

• Declare a StreamWriter object.

Dim SaveLog As IO.StreamWriter

• Set the StreamWriter object to use the AppendText method for the specified file in
parentheses.

SaveLog = IO.File.AppendText("c:/TextEditorLog.txt")

• Use the WriteLine method to write the Action (provided by method argument), Windows
Visa of the user and date to the log file. vbCrLf inserts a carriage return at the beginning
of each entry. This makes the log a little easier to read by inserting a blank line between
entries.

SaveLog.WriteLine(vbCrLf & Action & " " & My.User.Name _


& " " & Now.ToString)

• Use the WriteLine method to write the FileName (provided by method argument) to the
file.

SaveLog.WriteLine(FileName)

5
Cert IV Multimedia/Websites
Programming 2
File Handling and System.IO

• Close the StreamWriter object.

SaveLog.Close()

• Include an Else statement to handle the possibility that the specified log file does not
exists. In this case, the log file is created.

Else

• Declare a StreamWriter object.

Dim SaveLog As IO.StreamWriter

• Set the StreamWriter object to use the CreateText method for the specified file.

SaveLog = IO.File.CreateText("c:/TextEditorLog.txt")

• Use the WriteLine method to write the Action (provided by method argument), Windows
Visa of the user and date to the file. vbCrLf inserts a carriage return at the beginning of
each entry. This makes the log a little easier to read by inserting a blank line between
entries.

SaveLog.WriteLine(vbCrLf & Action & " " & My.User.Name _


& " " & Now.ToString)

• Use the WriteLine method to write the FileName (provided by method argument) to the
file.

SaveLog.WriteLine(FileName)

• Close the StreamWriter object.

SaveLog.Close()

• Every If requires an End If.

End If

Full code listing for the WriteFileLog procedure:

Private Sub WriteFileLog(ByVal Action As String, ByVal FileName As


String)

If IO.File.Exists("c:/TextEditorLog.txt") Then
Dim SaveLog As IO.StreamWriter
SaveLog = IO.File.AppendText("c:/TextEditorLog.txt")
SaveLog.WriteLine(vbCrLf & Action & " " & My.User.Name _
& " " & Now.ToString)
SaveLog.WriteLine(FileName)
SaveLog.Close()
Else
Dim SaveLog As IO.StreamWriter
SaveLog = IO.File.CreateText("c:/TextEditorLog.txt")
SaveLog.WriteLine(vbCrLf & Action & " " & My.User.Name _
& " " & Now.ToString)

6
Cert IV Multimedia/Websites
Programming 2
File Handling and System.IO

SaveLog.WriteLine(FileName)
SaveLog.Close()
End If

End Sub

Read File Log Procedure

Next we will create the procedure that will read the log file.

• Switch to code view and enter the following code to create the ReadFileLog procedure:

Private Sub ReadFileLog()

End Sub

This procedure does not require arguments.

• Enter the following lines of code into the ReadFileLog procedure:


• Check that the file we want to read exists using the Exists method.

If IO.File.Exists("c:/TextEditorLog.txt") Then

• Clear the contents of the Textbox to make way for the log file contents.

Me.txtEditor.Text = ""

• Declare a StreamReader object.

Dim ReadLog As IO.StreamReader

• Set the StreamReader to use the OpenText method for the specified file in parentheses.

ReadLog = IO.File.OpenText("c:/TextEditorLog.txt")

• Set the Text property of the Textbox to display the read contents of the file.

Me.txtEditor.Text &= ReadLog.ReadToEnd

• Close the StreamReader object.

ReadLog.Close()

• Every If requires an End If.

End If

Full code listing for the ReadFileLog procedure:

Private Sub ReadFileLog()

If IO.File.Exists("c:/TextEditorLog.txt") Then
Me.txtEditor.Text = ""
Dim ReadLog As IO.StreamReader
ReadLog = IO.File.OpenText("c:/TextEditorLog.txt")

7
Cert IV Multimedia/Websites
Programming 2
File Handling and System.IO

Me.txtEditor.Text &= ReadLog.ReadToEnd


ReadLog.Close()
End If

End Sub

Open Text File.

Now we will use the OpenFileDialog to navigate to the file we wish to open in the text editor.

• Double-click btnOpenFile and enter the following code:

With Me.OFDOpenFile
.FileName = ""
.InitialDirectory = "c:\My Documents"
.Filter = "Text Files|*.txt"
.ShowDialog()
End With

The OpenFileDialog control is set to open in the My Documents directory by default and will
only accept files with a .txt extension.

• Enter the following code immediately beneath:

If Me.OFDOpenFile.FileName <> "" Then


Dim objReader As IO.StreamReader
objReader = IO.File.OpenText(OFDOpenFile.FileName)
Me.txtEditor.Text = objReader.ReadToEnd
objReader.Close()
End If

After checking if the file Exists, a StreamReader object is declared to read the contents of the
file chosen with the OpenFileDialog.

• Enter the following code immediately beneath:

WriteFileLog("Opened by", OFDOpenFile.FileName)

Finally, we call the WriteFileLog procedure remembering that this procedure requires two string
arguments; the first being a phrase describing the Action being taken by the user and the
second being the FileName that was accessed. In this instance, the Action string being passed
is “Opened by’.

Full code listing for btnOpenFile click:

With Me.OFDOpenFile
.FileName = ""
.InitialDirectory = "c:\My Documents"
.Filter = "Text Files|*.txt"
.ShowDialog()
End With

If Me.OFDOpenFile.FileName <> "" Then


Dim objReader As IO.StreamReader
objReader = IO.File.OpenText(OFDOpenFile.FileName)
Me.txtEditor.Text = objReader.ReadToEnd

8
Cert IV Multimedia/Websites
Programming 2
File Handling and System.IO

objReader.Close()
End If

WriteFileLog("Opened by", Me.OFDOpenFile.FileName)

Save As Text File.

Now we will use the SaveFileDialog to save the text in the text editor as a text file.

• Double-click btnSaveFileAs and enter the following code:

With Me.sfdSaveFile
.FileName = ""
.InitialDirectory = "c:\My Documents"
.Filter = "Text Files|*.txt"
.ShowDialog()
End With

The SaveFileDialog is set to open in My Documents by default and will only accept file names
with a .txt extension.

If Me.sfdSaveFile.FileName <> "" Then


Dim objWriter As IO.StreamWriter
objWriter = IO.File.CreateText(sfdSaveFile.FileName)
objWriter.Write(Me.txtEditor.Text)
objWriter.Close()
End If

After checking if the file Exists, a StreamWriter object is declared to create the file name chosen
with the SaveFileDialog.The contents of the Textbox is then written to the file.

• Enter the following code immediately beneath:

WriteFileLog("Saved by", sfdSaveFile.FileName)

Finally, we call the WriteFileLog procedure remembering that this procedure requires two string
arguments; the first being a phrase describing the Action being taken by the user and the
second being the FileName that was accessed. In this instance, the Action string being passed
is “Saved by’.

Full code listing for btnSaveFileAs Click:

With Me.sfdSaveFile
.FileName = ""
.InitialDirectory = "c:\My Documents"
.Filter = "Text Files|*.txt"
.ShowDialog()
End With

If Me.sfdSaveFile.FileName <> "" Then


Dim objWriter As IO.StreamWriter
objWriter = IO.File.CreateText(sfdSaveFile.FileName)
objWriter.Write(Me.txtEditor.Text)
objWriter.Close()
End If

9
Cert IV Multimedia/Websites
Programming 2
File Handling and System.IO

WriteFileLog("Saved by", Me.sfdSaveFile.FileName)

Open the Log File

Next we will view the contents of the log file in the Textbox.

• Double-click btnOpenLog and enter the following code in the click event:

ReadFileLog()

Now all we need to do is call the ReadFileLog procedure we created earlier.

Clear the Textbox.

Next we will add code to clear the Textbox.

• Double-click btnClearTextbox and enter the following code in the click event:

txtEditor.Text = ""

Run the project.

• Run the project


• Click btnOpenFile and navigate to the text file you wish to open.
• After viewing the contents of the file in the Textbox, click btnSaveFileAs and save the file
under a new file name.
• Click btnClearTextbox, type some text in the Textbox and click btnSaveFileAs to save
the text to a text file.
• Click btnOpenLog to read the contents of the log into the Textbox. The log has recorded
all file access activty, by whom and when.

10

You might also like