C# FileStream Explained - The FileStream Class
(Page 2 of 5 )
The FileStream class inherits the Stream class to provide byte reading/writing operations to and from a source file. The FileStream class provides synchronous reading/writing methods as well asynchronous reading/writing methods. This class also provides a seeking functionality; I'll explain more about that later. You can construct a FileStream object in many ways, so let's look at some of them.
- FileStream fStream = File.OpenWrite("theFile.txt");
You can have a FileStream object using the method File.OpenWrite(), which is passed the name of the file as a string value, then returns a FileStream object with FileAccess.Write. Notice that this method opens the file if it already exists or creates the file if it doesn't exist. Also note that you can't read from this FileStream object because it has been opened for write operations only. If you tried to read from this file, an exception of type NotSupportedException will be thrown.
- FileStream fStream = File.OpenRead("theFile.txt");
You can return a FileStream object using the method File.OpenRead() which is passed the name of the file as a string value. Notice that this method opens the file, and if the file does not exist, an exception of type FileNotFoundException is thrown. Also note that you can't write to this FileStream object because it has been opened for read operations only. If you tried to write to this file an exception of type NotSupportedException will be thrown.
- The method FileInfo.OpenWrite() instance method returns a FileStream object with write access, and the method FileInfo.OpenRead() returns a FileStream object with read access.
- Using one of the FileStream object constructors:
FileStream fStream = new FileStream("theFile.txt",FileMode.OpenOrCreate,
FileAccess.Write,FileShare.None);
This overload accepts the file name, FileMode enumeration value, FileAccess enumeration value and FileShare enumeration value. Let's take a look at the FileMode, FileAccess and FileShare enumerations that are used with FileStream objects.
The Enumerations
The following two tables list the enumerations that are used with FileStream objects to set file mode and file access. Let's begin with the FileMode enumeration.
| FileMode Enumeration | Description |
| Create | FileMode.Create value creates a file and if the file already exists, it will be overwritten. |
| CreateNew | Creates a file and if the file already exists, an IOException is thrown. |
| Append | Opens the file and starts writing at the end of the file. If the file doesn't exist a file is created. Note that you can use FileMode.Append only with FileAccess.Write or an exception of type ArgumentException is thrown. |
| Open | Opens the file if it exists. If the file does not exist, an exception of type FileNotFoundException is thrown. |
| OpenOrCreate | Opens the file if it exists. If the file does not exist, the file will be created. |
| Truncate | Opens a file and truncates it to make it a size of zero bytes. |
The FileAccess enumeration values
| FileAccess Enumeration | Description |
| Read | Sets read access to the file. |
| Write | Sets write access to the file. |
| ReadWrite | Sets read/write access to the file. |
The last enumeration is the FileShare enumeration, which provides values for how the file is shared with other streams by the running process or other process.
| FileShare Enumeration | Description |
None | Obtains an exclusive access to the file until it's closed, which means that other streams can't read from or write to the file until this stream is closed. |
| Read | Other streams (in the current process or in another one) can obtain read access to the file. |
| Write | Other streams (in the current process or in another one) can obtain write access to the file |
| ReadWrite | Other streams can obtain ReadWrite access to the file |
Next: Writing to the file using FileStream methods >>
More C# Articles
More By Michael Youssef