Cert IV Multimedia/Websites Programming 2 File Handling and System - IO
Cert IV Multimedia/Websites Programming 2 File Handling and System - IO
Programming 2
File Handling and System.IO
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.
Provides methods for working Implements a text reader Implements a text writer
with text files such as:
- creation
- deletion
- moving
- copying
- opening
Control: Properties:
Name: btnCreateFile Text: Create File
1
Cert IV Multimedia/Websites
Programming 2
File Handling and System.IO
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")
Activity 2:
Use IO.StreamWriter to create a text file with content.
• Double-click btnWriteFile and enter the following code in the click event:
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:
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:
Activity 5:
Use IO.File.Move to move a text file.
IO.File.Move(Source, destination)
IO.File.Move(("c:\MyCreateFile.txt"), _
("c:\MyTextFiles\ MyCreateFile.txt"))
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"))
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")
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.
Control: Properties:
Name: btnOpenFile Text: Open File
4
Cert IV Multimedia/Websites
Programming 2
File Handling and System.IO
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:
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.
If IO.File.Exists("c:/TextEditorLog.txt") Then
• 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.
• 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
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
• 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.
• Use the WriteLine method to write the FileName (provided by method argument) to the
file.
SaveLog.WriteLine(FileName)
SaveLog.Close()
End If
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
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:
End Sub
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 = ""
• 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.
ReadLog.Close()
End If
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
End Sub
Now we will use the OpenFileDialog to navigate to the file we wish to open in the text editor.
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.
After checking if the file Exists, a StreamReader object is declared to read the contents of the
file chosen with the OpenFileDialog.
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’.
With Me.OFDOpenFile
.FileName = ""
.InitialDirectory = "c:\My Documents"
.Filter = "Text Files|*.txt"
.ShowDialog()
End With
8
Cert IV Multimedia/Websites
Programming 2
File Handling and System.IO
objReader.Close()
End If
Now we will use the SaveFileDialog to save the text in the text editor as a text file.
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.
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.
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’.
With Me.sfdSaveFile
.FileName = ""
.InitialDirectory = "c:\My Documents"
.Filter = "Text Files|*.txt"
.ShowDialog()
End With
9
Cert IV Multimedia/Websites
Programming 2
File Handling and System.IO
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()
• Double-click btnClearTextbox and enter the following code in the click event:
txtEditor.Text = ""
10