Basics of .NET Framework File I/O and the File System (Visual Basic)
Classes in the System.IO namespace are used to work with drives, files, and directories.
The System.IO namespace contains the File and Directory classes, which provide the .NET Framework functionality that manipulates files and directories. Because the methods of these objects are static or shared members, you can use them directly without creating an instance of the class first. Associated with these classes are the FileInfo and DirectoryInfo classes, which will be familiar to users of the My feature. To use these classes, you must fully qualify the names or import the appropriate namespaces by including the Imports statement(s) at the beginning of the affected code. For more information, see Imports Statement (.NET Namespace and Type).
Note
Other topics in this section use the My.Computer.FileSystem object instead of System.IO classes to work with drives, files, and directories. The My.Computer.FileSystem object is intended primarily for use in Visual Basic programs. System.IO classes are intended for use by any language that supports the .NET Framework, including Visual Basic.
Definition of a Stream
The .NET Framework uses streams to support reading from and writing to files. You can think of a stream as a one-dimensional set of contiguous data, which has a beginning and an end, and where the cursor indicates the current position in the stream.
Stream Operations
The data contained in the stream may come from memory, a file, or a TCP/IP socket. Streams have fundamental operations that can be applied to them:
Reading. You can read from a stream, transferring data from the stream into a data structure, such as a string or an array of bytes.
Writing. You can write to a stream, transferring data from a data source into the stream.
Seeking. You can query and modify your position in the stream.
For more information, see Composing Streams.
Types of Streams
In the .NET Framework, a stream is represented by the Stream class, which forms the abstract class for all other streams. You cannot directly create an instance of the Stream class, but must use one of the classes it implements.
There are many types of streams, but for the purposes of working with file input/output (I/O), the most important types are the FileStream class, which provides a way to read from and write to files, and the IsolatedStorageFileStream class, which provides a way to create files and directories in isolated storage. Other streams that can be used when working with file I/O include:
The following table lists tasks commonly accomplished with a stream:
To |
See |
Read and write to a data file |
|
Read text from a file |
|
Write text to a file |
|
Read characters from a string |
|
Write characters to a string |
|
Encrypt data |
|
Decrypt data |
File Access and Attributes
You can control how files are created, opened, and shared with the FileAccess, FileMode, and FileShare enumerations, which contain the flags used by the constructors of the FileStream class. For example, when you open or create a new FileStream, the FileMode enumeration allows you to specify whether the file is opened for appending, whether a new file is created if the specified file does not exist, whether the file is overwritten, and so forth.
The FileAttributes enumeration enables the gathering of file-specific information. The FileAttributes enumeration returns the file's stored attributes, such as whether it is compressed, encrypted, hidden, read-only, an archive, a directory, a system file, or a temporary file.
The following table lists tasks involving file access and file attributes:
To |
See |
Open and append text to a log file |
|
Determine the attributes of a file |
File Permissions
Controlling access to files and directories can be done with the FileIOPermission class. This may be particularly important for developers working with Web Forms, which by default run within the context of a special local user account named ASPNET, which is created as part of the ASP.NET and .NET Framework installations. When such an application requests access to a resource, the ASPNET user account has limited permissions, which may prevent the user from performing actions such as writing to a file from a Web application. For more information, see Security Permissions, and the FileIOPermission.
Isolated File Storage
Isolated storage is an attempt to solve problems created when working with files where the user or code may lack necessary permissions. Isolated storage assigns each user a data compartment, which can hold one or more stores. Stores can be isolated from each other by user and by assembly. Only the user and assembly that created a store have access to it. A store acts as a complete virtual file system—within one store you can create and manipulate directories and files.
The following table lists tasks commonly associated with isolated file storage.
To |
See |
Create an isolated store |
|
Enumerate isolated stores |
|
Delete an isolated store |
|
Create a file or directory in isolated storage |
|
Find a file in isolated storage |
How to: Find Existing Files and Directories in Isolated Storage |
Read from or write to a file in insolated storage |
|
Delete a file or directory in isolated storage |
File Events
The FileSystemWatcher component allows you to watch for changes in files and directories on your system or on any computer to which you have network access. For example, if a file is modified, you might want to send a user an alert that the change has taken place. When changes occur, one or more events are raised, stored in a buffer, and handed to the FileSystemWatcher component for processing.
See Also
Concepts
Classes Used in .NET Framework File I/O and the File System (Visual Basic)