Changes

Jump to: navigation, search

Working with Files in Visual Basic

1,508 bytes added, 15:30, 10 August 2007
New page: 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 fil...
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 ===

<table border="1" cellspacing="0">
<th>Mode<th>Description</th>
<tr>
<td>Append<td>If the file exists it is opened. Any writes are appended to the end of the file. Requires File.Access.Write mode</td>
<tr>
<td>Create<td>Creates a new file, removing old file if it already exists</td>
<tr>
<td>CreateNew<td>Creates a new file and returns error if file already exists</td>
<tr>
<td>Open<td>Opens an existing file. Returns error if file does not exist</td>
<tr>
<td>OpenOrCreate<td>If file already exists it is opened, otherwise a new file is created</td>
<tr>
<td>Truncate<td>Opens an existing file and deletes all existing content</td>
</table>

Navigation menu