C#
  Home arrow C# arrow Page 5 - C# FileStream Explained
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 
Mobile Linux 
App Generation ROI 
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 / 24
    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
     
     
    ADVERTISEMENT


    C# FileStream Explained - Use the Seek() method


    (Page 5 of 5 )

    The FileStream class provides the Seek() method which changes the position of the stream in a relative way to one of three values: in relation to the beginning of the stream (using the enumeration SeekOrigin.Begin value), in relation to the current position of the stream (SeekOrigin.Current) and in relation to the end of the stream (SeekOrigin.End). Let's look at an example that explains how you can use this method.

    using System;
    using System.IO;

    namespace MyStreams
    {
      class Class1
      {
        public static void Main()
        {
          try
          {
            using(FileStream fStream = File.OpenRead("aFile.txt"))
            {
              // using SeekOrigin.Begin
              Console.WriteLine("Current Position: {0}",
    fStream.Position);
              Console.WriteLine("Calling Seek(1,SeekOrigin.Begin)");
    fStream.Seek(1,SeekOrigin.Begin);
              Console.WriteLine("Current Position: {0}",
    fStream.Position);
              Console.WriteLine("Reading bytes");
              while(fStream.Position < fStream.Length)
              {
                Console.Write(" {0} ", fStream.ReadByte());
              }

              // using SeekOrigin.Current
              fStream.Position = 0;
              Console.WriteLine("nnCurrent Position: {0}",
    fStream.Position);
              Console.WriteLine("Calling Seek
    (2,SeekOrigin.Current)");
              fStream.Seek(2,SeekOrigin.Current);
              Console.WriteLine("Current Position: {0}",
    fStream.Position);
              Console.WriteLine("Reading bytes");
              while(fStream.Position < fStream.Length)
              {
                Console.Write(" {0} ", fStream.ReadByte());
              }

              // using SeekOrigin.End
              fStream.Position = 0;
              Console.WriteLine("nnCurrent Position: {0}",
    fStream.Position);
              Console.WriteLine("Calling Seek(-1,SeekOrigin.End)");
    fStream.Seek(-1,SeekOrigin.End);
              Console.WriteLine("Current Position: {0}",
    fStream.Position);
              Console.WriteLine("Reading bytes");
              while(fStream.Position < fStream.Length)
              {
                Console.Write(" {0} ", fStream.ReadByte());
              }
            }
            Console.ReadLine();
          }
          catch(IOException ex)
          {
            Console.WriteLine(ex.Message);
          }
        }
      }
    }

    The idea of our example is to use the Seek() method to change the current position in the stream and then read the available bytes until we reach the end of the stream. As you know, our file contains the bytes 65 66 67 68. The first Seek method (Seek(1, SeekOrigin.Begin) moves the position of the stream by one relative to the beginning of the stream, causing the reading operation to read only the three remaining bytes. Note that we set the position of the stream using the FileStream.Position to zero before using the next Seek() method.

    The second Seek() method moves the position by two relative to SeekOrigin.Current (currently equal to the first byte because we just assigned 0 to the Position property). This means that the read operation will begin at the third byte. The last Seek() method moves the position in the stream to -1 of the end of the stream (SeekOrigin.End), which means that it is reading only the last byte in the stream.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





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