Advertisement



< Prev
Next >



C# - FileMode and FileAccess Enums




C# language allows us to perform important file input/output operations such as - C# provides us a number of file modes to open a file in, which are specified in the FileMode enum and a number of file access modes, which allow us to perform the specific file operation we want to perform, specified in the FileAccess enum. Let us discuss these enums.


Advertisement




FileMode Enum


There is a specific file mode in which we can open a file. Let's see all these different modes in which we could open a file on disk, which are specified in the FileMode Enum.

File Modes Description
FileMode.Open
Opens an existing file and FileNotFoundException exception is thrown if the file does not exist.

FileMode.OpenOrCreate
Opens an existing file. If the file is not found, a new file is created.

FileMode.Create
Creates a new file and any existing file with the same name is overwritten.

FileMode.CreateNew
Creates a new file and if there is already an existing file with the same name then an IOException is thrown.

FileMode.Truncate
Opens an existing file and truncates its size to zero bytes. If the file does not exist, the FileNotFoundException exception is thrown.

FileMode.Append
Opens an existing file in an append mode and seeks to the end of the file. A new file is created, if the searched file doesn't exist.

  • It only allows us to perform the write operation on a file(doesn't allow the read operation on the file).


  • Trying to seek to a position before the end of the file to read it, throws an IOException exception, because appending to a file doesn't allow a reading operation.


  • Using the FileMode.Append can be used only in conjunction with FileAccess.Write, trying to use both FileMode.Append with FileAccess.Read(read about it in the next section) mode throws a NotSupportedException exception.




All the above described file modes allow us to perform read and write operations on the files with permission to both read and write operations. But, if we have to perform read or write operations to the read-only or write-only files then we have to use the fields of FileAccess enum described below.




FileAccess Enum


The FileAccess Enum provides us various file access fields for read, write, or read/write access to a file.

FileAccess Modes Description
FileAccess.Read
This mode gives us the read access to the file, so that data can be read from the file.

This FileAccess allows us to read the read-only files i.e. files that we are not allowed to write to.

FileAccess.Write
This mode gives us the write access to the file, so that we can write data to the file.

This FileAccess allows us to write to the write-only files i.e. files that we are now allowed to read from.

FileAccess.ReadWrite
This mode gives us the read and write access to the file, so that we can read and write data to the file.

This FileAccess allows us to read and write to files with permission to both read and write operations.


By combining an appropriate FileMode field with FileAccess mode, we could perform multiple file operations such as - create, read, write, append, modify a file, etc. We will see how to perform these file operations in the upcoming tutorials.




Please share this article -





< Prev
Next >
< C# FileStream
C# StreamWriter >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement