SunQuest
 
       C#
  Home arrow C# arrow Page 2 - C# FileStream Explained
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C# FileStream Explained
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 20
    2007-01-31

    Table of Contents:
  • C# FileStream Explained
  • The FileStream Class
  • Writing to the file using FileStream methods
  • Reading from the file using FileStream methods
  • Use the Seek() method

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    IBM developerWorks
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    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.

    1. 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.
    2. 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.
    3. 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.
    4. 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

    CreateFileMode.Create value creates a file and if the file already exists, it will be overwritten.
    CreateNewCreates 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.
    OpenOpens the file if it exists. If the file does not exist, an exception of type FileNotFoundException is thrown.
    OpenOrCreateOpens the file if it exists. If the file does not exist, the file will be created.
    TruncateOpens a file and truncates it to make it a size of zero bytes.

    The FileAccess enumeration values

    FileAccess Enumeration

    Description

    ReadSets read access to the file.
    WriteSets write access to the file.
    ReadWriteSets 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.
    ReadOther streams (in the current process or in another one) can obtain read access to the file.
    WriteOther streams (in the current process or in another one) can obtain write access to the file
    ReadWriteOther streams can obtain ReadWrite access to the file

    More C# Articles
    More By Michael Youssef


       · Do you know what a FileStream instance is? No? Ok read the article :)
       · I just can't stop reading your articles. Can't you write a book about C# please? I'm...
       · Hi Marie,I wish I can write a book but this needs time and there are many...
       · Hi, This is nice to study ur article.Your explanation is very good.If u...
       · I found this article very useful to get to know about the File Streams in a quick...
       · Hello Sarav,What exactly the topics you would like me to write about?Have you...
     

    C# ARTICLES

    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway