This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner.
The FileStream class extends the Stream class and provides access to the standard input, output, and error streams for files. It is used extensively when dealing with file I/O. It implements all the methods and properties of the Stream class. FileStream implements two new methods: Lock and Unlock. Lock is used to lock up the underlying file exclusively for the current process, whereas Unlock frees this lock. Random access to files is also provided in this stream.
Nine overloaded constructors help you gain finer control over the file states. The constructor most commonly used, shown in Listing 6.7, helps set the various access permissions and creation states on the file through the use of the FileMode, FileAccess, and FileShare enumerations.
Listing 6.7: FileStream Constructor
public FileStream(string path,FileMode mode,FileAccess access, FileShare share);
FileMode Enumeration
The FileMode enumeration helps you the set the mode in which you want to open the file. You can use these modes to set your file up for appending or overwriting or initial creation, as detailed in Table 6.8.
Table 6.8: Enumeration FileMode
Note that the Append mode can only be used when FileAccess.Write permission (described next) is set.
FileAccess Enumeration
With the FileAccess enumerations described in Table 6.9, you can set the mode of access to a file. It's never good to authorize more access than needed-or less access than needed, for that matter. Choose Read when you intend to read from a file and Write when you write to a file. Remember, though, that if you specify Read access and later try to write to the file, an exception will be raised. The same applies when you specify Write access and try to read the file later.
Table 6.9: Enumeration FileAccess
FileShare Enumeration
The FileShare enumeration, detailed in Table 6.10, is very important if you wish to share your file with other processes. For example, suppose you have an XML file acting as a database file for an ASP.NET application. If you don't specify the FileAccess enumeration, only one user can read from the XML database file at a time; other concurrent users encounter an error when accessing the database because the FileShare.None enumeration is implemented by default.
Table 6.10: Enumeration FileShare
The short example in Listing 6.8 demonstrates how you can work with the FileStream class.
Listing 6.8: FileStream Reading and Writing Example
using System;
using System.IO;
public class FileCopy
{
public static void Main(string[] args)
{
if (args.Length < 2)
{
//Show usage information if incorrect number
//of parameters has been entered
Console.WriteLine("Usage: FileCopy <SourceFile><DestinationFile>");
return;
}
try
{
//Open a FileStream to the source file
FileStream fin = new FileStream(args[0], FileMode.Open,
FileAccess.Read, FileShare.Read);
//Open a FileStream to the destination file
FileStream fout = new FileStream(args[1], FileMode.OpenOrCreate,
FileAccess.Write, FileShare.None);
//Create a byte array to act as a buffer
Byte[] buffer = new Byte[32];
Console.WriteLine("File Copy Started");
//Loop until end of file is not reached
while (fin.Position != fin.Length)
{
//Read from the source file
//The Read method returns the number of bytes read
int n = fin.Read(buffer, 0, buffer.Length);
//Write the contents of the buffer to the destination file
fout.Write(buffer, 0, n);
}
//Flush the contents of the buffer to the file
fout.Flush();
//Close the streams and free the resources
fin.Close();
fout.Close();
Console.WriteLine("File Copy Ended");
}
catch (IOException e)
{
//Catch a IOException
Console.WriteLine("An IOException Occurred :" + e);
}
catch (Exception e)
{
//Catch any other exception that occurs
Console.WriteLine("An Exception Occurred :" + oe);
}
Console.ReadLine();
}
}
Similar to the copy command used in MS-DOS, this example takes two user-supplied parameters: source file and destination file. The program then opens a FileStream on the source file and reads it into the buffer. The contents of the buffer are written to the destination file.
Conclusion
Hope this article would have helped you in understanding FileStream in C#. See other articles on the website on .NET and C#.
|
The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer. |