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).
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.
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:
|Read and write to a data file||How to: Read and Write to a Newly Created Data File|
|Read text from a file||How to: Read Text from a File|
|Write text to a file||How to: Write Text to a File|
|Read characters from a string||How to: Read Characters from a String|
|Write characters to a string||How to: Write Characters to a String|
|Encrypt data||Encrypting Data|
|Decrypt data||Decrypting 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:
|Open and append text to a log file||How to: Open and Append to a Log File|
|Determine the attributes of a file||FileAttributes|
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 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.
|Create an isolated store||How to: Obtain Stores for Isolated Storage|
|Enumerate isolated stores||How to: Enumerate Stores for Isolated Storage|
|Delete an isolated store||How to: Delete Stores in Isolated Storage|
|Create a file or directory in isolated storage||How to: Create Files and Directories 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||How to: Read and Write to Files in Isolated Storage|
|Delete a file or directory in isolated storage||How to: Delete Files and Directories in Isolated Storage|
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.