Difference between revisions of "Working with Files in Visual Basic"
(→Writing to a File) |
|||
Line 61: | Line 61: | ||
Note that the above code example assumes that the 'C:\Temp' directory already exists. If it does not the code will fail. Working with Directories is covered in [[Working with Directories in Visual Basic]] | Note that the above code example assumes that the 'C:\Temp' directory already exists. If it does not the code will fail. Working with Directories is covered in [[Working with Directories in Visual Basic]] | ||
− | == Writing to a File == | + | == Writing to a File with Visual Basic == |
+ | |||
+ | Once a file has been opened with the appropriate options, it can be written to using the Visual Basic ''StreamWriter'' class. The ''StreamWriter'' constructor takes a ''FileStream'' as the sole parameter. | ||
+ | |||
+ | The ''Write()'' and ''WriteLine()'' methods of the ''StreamWriter'' class are then used to write to the file. ''Write()'' writes the text with no new line appended to the end of each line. ''WriteLine()'' on the other hand, appends a new line to end of each line written to the file: | ||
+ | |||
+ | <pre> |
Revision as of 15:54, 10 August 2007
In the two previous chapters we looked at accessing databases using Visual Basic. Often, a database provides more complexity and functionality than is needed and sometimes a plain text file is more than enough for storing information. In this chapter, therefore, we will look at how to work with files and directories in Visual Basic.
Opening a Text File in Visual Basic
The first step in working with files in Visual Basic is to open the file. This is achieved using the Visual Basic FileStream class. The FileStream constructor accepts the file name to be opened as the first parameter, followed by a number of other parameters defining the mode in which the file is to be opened. These fall into the categories of FileMode, FileAccess and FileShare. The options available as listed in the following tables:
FileMode Options
Mode | Description |
---|---|
Append | If the file exists it is opened. Any writes are appended to the end of the file. Requires FileAccess.Write mode |
Create | Creates a new file, removing old file if it already exists |
CreateNew | Creates a new file and returns error if file already exists |
Open | Opens an existing file. Returns error if file does not exist |
OpenOrCreate | If file already exists it is opened, otherwise a new file is created |
Truncate | Opens an existing file and deletes all existing content |
FileAccess Options
Mode | Description |
---|---|
Read | Opens the file for reading only. |
ReadWrite | Opens the file for both reading and writing |
Write | Opens the file to writing only |
Mode | Description |
---|---|
None | The file cannot be opened by any other program until it is closed by the current program |
Read | Other programs may simultaneously open and read from the file, but not write to it. |
ReadWrite | Other programs may simultaneously open and read and write from/to the file. |
Write | Other programs may simultaneously open and write to the file, but not read from it. |
With the following options in mind, the following code excerpt opens 'C:\Temp\text.txt' in FileMode.OpenOrCreate with FileAccess.ReadWrite permission and no file sharing, and then closes it:
Dim textFileStream As New IO.FileStream("C:\Temp\test.txt", IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.None) textFileStream.Close()
Note that the above code example assumes that the 'C:\Temp' directory already exists. If it does not the code will fail. Working with Directories is covered in Working with Directories in Visual Basic
Writing to a File with Visual Basic
Once a file has been opened with the appropriate options, it can be written to using the Visual Basic StreamWriter class. The StreamWriter constructor takes a FileStream as the sole parameter.
The Write() and WriteLine() methods of the StreamWriter class are then used to write to the file. Write() writes the text with no new line appended to the end of each line. WriteLine() on the other hand, appends a new line to end of each line written to the file: