ASP.NET

FileStream in Asp.Net

FileStream in Asp.Net

FileStream in Asp.Net

The FileStream Class in C# provides a stream for file operations. It can be used to perform both Synchronous and Asynchronous Read and Write operations. With the help of FileStream Class, we can easily read and write data into files.
It is inclueded in System.IO namespace


Parameters in FileStream Constructor
path: A relative or absolute path for the file that the current FileStream object will encapsulate.
mode: A constant that determines how to open or create the file.
access: A constant that determines how the file can be accessed by the FileStream object. This also determines the values returned by the System.IO.FileStream.CanRead and System.IO.FileStream.CanWrite properties of the FileStream object. System.IO.FileStream.CanSeek is true if the path specifies a disk file.
share: A constant that determines how the file will be shared by processes.
The path parameter is nothing but a string value that contains the path where the file is going to be created or the path of an existing file whose data we want to access. The rest three parameters i.e. FileMode.
FileAccess and FileShare are Enums

Mode Parameter supports 6 possible values.  
CreateNew: It specifies that the operating system should create a new file. This requires a System.Security.Permissions.FileIOPermissionAccess.Write permission. If the file already exists, a System.IO.IOException exception is thrown.
Create: It specifies that the operating system should create a new file like the CreateNew constant. But in this case, if the file already exists, it will be overwritten instead of throwing an Exception. This also requires System.Security.Permissions.FileIOPermissionAccess.Write permission. So, FileMode.Create is equivalent to requesting that if the file does not exist, use System.IO.FileMode.CreateNew; otherwise, use System.IO.FileMode.Truncate. If the file already exists but is a hidden file, then an UnauthorizedAccessException Exception is going to be thrown.
Open: It specifies that the operating system should open an existing file. The ability to open the file is dependent on the value specified by the System.IO.FileAccess Enumeration. A System.IO.FileNotFoundException exception is thrown if the file does not exist.
OpenOrCreate: It specifies that the operating system should open a file if it exists; otherwise, a new file should be created. If the file is opened with FileAccess.Read, System.Security.Permissions.FileIOPermissionAccess.Read permission is required. If the file access is FileAccess.Write, System.Security.Permissions.FileIOPermissionAccess.Write permission is required. If the file is opened with FileAccess.ReadWrite, both Systems.Security.Permissions.FileIOPermissionAccess.Read and System.Security.Permissions.FileIOPermissionAccess.Write permissions are required.
Truncate: It specifies that the operating system should open an existing file. When the file is opened, it should be truncated so that its size is zero bytes. This requires a System.Security.Permissions.FileIOPermissionAccess.Write permission. Attempts to read from a file opened with FileMode.Truncate causes a System.ArgumentException exception.
Append: It opens the file if it exists and then adds the content at the end of the file, or creates a new file. This requires a System.Security.Permissions.FileIOPermissionAccess.Append permission. FileMode.Append can be used only in conjunction with FileAccess.Write. Trying to seek a position before the end of the file throws a System.IO.IOException exception, and any attempt to read fails and throws a System.NotSupportedException exception.


FileAccess Parameter supports 3 possible values.  
Read– It gives read access to the file. Data can be read from the file. Combine with Write for read/write access.
Write – It gives Write access to the file. Data can be written to the file. Combine with Read for read/write access.
ReadWrite – It gives read and writes access to the file. Data can be written to and read from the file.


FileShare Parameter supports 6 possible values.  
It contains constants for controlling the kind of access other FileStream objects can have to the same file
None: Declines sharing of the current file. Any request to open the file (by this process or another process) will fail until the file is closed.
Read: Allows subsequent opening of the file for reading. If this flag is not specified, any request to open the file for reading (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.
Write: Allows subsequent opening of the file for writing. If this flag is not specified, any request to open the file for writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.
ReadWrite: Allows subsequent opening of the file for reading or writing. If this flag is not specified, any request to open the file for reading or writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.
Delete: Allows subsequent deleting of a file.
Inheritable: Makes the file handle inheritable by child processes. This is not directly supported by Win32.



Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.

Instagram