Seek from Begin

   
 


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;



class Program {
    static void Main(string[] args) {
        byte[] byData = new byte[200];
        char[] charData = new Char[200];

        try {
            FileStream aFile = new FileStream("Program.cs", FileMode.Open);
            aFile.Seek(135, SeekOrigin.Begin);
            aFile.Read(byData, 0, 200);
        } catch (IOException e) {
            Console.WriteLine("An IO exception has been thrown!");
            Console.WriteLine(e.ToString());
            Console.ReadKey();
            return;
        }

        Decoder d = Encoding.UTF8.GetDecoder();
        d.GetChars(byData, 0, byData.Length, charData, 0);

        Console.WriteLine(charData);
    }
}

    


Using FileStreams

/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
using System.IO;

namespace Client.Chapter_11___File_and_Streams
{
public class UsingFileStreams {
static void Main(string[] args)
{
//Creates a file with read-write access that allows others to read
FileStream MyFileStream1 = new FileStream(@”c:ProjectsTesting.txt”, FileMode.Create);
FileInfo MyFiles = new FileInfo(@”c:ProjectsTesting.txt”);
FileStream MyFileStream2 = MyFiles.OpenRead();

//or any of the following
MyFileStream2 = MyFiles.OpenWrite();
MyFileStream2 = MyFiles.Open(FileMode.Append, FileAccess.Read, FileShare.None);
MyFileStream2 = MyFiles.Create();

//You can read file stream ona pe byte basis or as an array of bytes
int MyBytes = MyFileStream1.ReadByte();

//or
int NumberOfBytes = 200;
byte[] MyByteArray = new Byte[NumberOfBytes];
int BytesRead = MyFileStream1.Read(MyByteArray, 0, NumberOfBytes);

//Data can be written to FileStreams as well through bytes or arrays of //bytes
byte MyWriteByte = 100;

MyFileStream1.WriteByte(MyWriteByte);

//or via an array
int NumberOfBytesToWrite = 256;
byte[] MyWriteByteArray = new Byte[NumberOfBytesToWrite];

for (int i = 0; i < 256; i++) { MyWriteByteArray[i] = (byte)i; i++; } MyFileStream1.Write(MyWriteByteArray, 0, NumberOfBytesToWrite); MyFileStream1.Close(); MyFileStream2.Close(); } } } [/csharp]

Compare two files

   

/*
C# A Beginner&#039;s Guide
By Schildt

Publisher: Osborne McGraw-Hill
ISBN: 0072133295
*/
/*   
   Project 11-1 
 
   Compare two files. 
 
   To use this program, specify the names   
   of the files to be compared on the command line. 
 
   For example: 
       CompFile FIRST.TXT SECOND.TXT  
*/  
  
using System; 
using System.IO;  
  
public class CompFiles {  
  public static void Main(string[] args) {  
    int i=0, j=0;  
    FileStream f1;  
    FileStream f2;  
  
    try {  
      // open first file 
      try {  
        f1 = new FileStream(args[0], FileMode.Open);  
      } catch(FileNotFoundException exc) {  
        Console.WriteLine(exc.Message);  
        return;  
      }  
  
      // open second file  
      try {  
        f2 = new FileStream(args[1], FileMode.Open);  
      } catch(FileNotFoundException exc) {  
        Console.WriteLine(exc.Message);  
        return;  
      }  
    } catch(IndexOutOfRangeException exc) {  
      Console.WriteLine(exc.Message + "
Usage: CompFile f1 f2");  
      return;  
    }  
  
    // Compare files  
    try {  
      do {  
        i = f1.ReadByte();  
        j = f2.ReadByte();  
        if(i != j) break; 
      } while(i != -1 &amp;&amp; j != -1);  
    } catch(IOException exc) {  
      Console.WriteLine(exc.Message);  
    }  
    if(i != j)  
      Console.WriteLine("Files differ."); 
    else 
      Console.WriteLine("Files are the same."); 
  
    f1.Close();  
    f2.Close();  
  }  
}



           
          


A simple disk-to-screen utility that demonstrates a FileReader


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

/* A simple disk-to-screen utility that 
   demonstrates a FileReader. */ 
 
using System; 
using System.IO;  
 
public class DtoS { 
  public static void Main() { 
    FileStream fin; 
    string s; 
 
    try { 
      fin = new FileStream("test.txt", FileMode.Open); 
    } 
    catch(FileNotFoundException exc) { 
      Console.WriteLine(exc.Message + "Cannot open file."); 
      return ; 
    }  
 
    StreamReader fstr_in = new StreamReader(fin); 
 
    // Read the file line-by-line. 
    while((s = fstr_in.ReadLine()) != null) { 
      Console.WriteLine(s); 
    } 
 
    fstr_in.Close(); 
  } 
}


           
          


illustrates use of FileStreams 2


   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

 /*
  Example15_11.cs illustrates use of FileStreams
*/

using System;
using System.Windows.Forms;
using System.IO;

public class Example15_11 
{
    [STAThread]
  public static void Main() 
  {

    // use an open file dialog to get a filename
    OpenFileDialog dlgOpen = new OpenFileDialog();
    dlgOpen.Title="Select file to back up";

    if (dlgOpen.ShowDialog() == DialogResult.OK)
    {
      FileStream inStream = File.OpenRead(dlgOpen.FileName);
      FileStream outStream = 
        File.OpenWrite(dlgOpen.FileName + ".bak");
      byte[] buf = new byte[4096];
      int bytesRead;

      // copy all data from in to out
      while ((bytesRead = inStream.Read(buf, 0, 4096)) > 0)
        outStream.Write(buf, 0, bytesRead);

      // clean up
      outStream.Flush();
      outStream.Close();
      inStream.Close();

    }

  }

}



           
          


illustrates use of FileStreams


   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

 /*
  Example15_10.cs illustrates use of FileStreams
*/

using System;
using System.Windows.Forms;
using System.IO;

public class Example15_10 
{
    [STAThread]
  public static void Main() 
  {

    // use an open file dialog to get a filename
    OpenFileDialog dlgOpen = new OpenFileDialog();
    dlgOpen.Title="Select file to back up";

    if (dlgOpen.ShowDialog() == DialogResult.OK)
    {
      FileStream inStream = File.OpenRead(dlgOpen.FileName);
      FileStream outStream = 
       File.OpenWrite(dlgOpen.FileName + ".bak");
      int b;

      // copy all data from in to out
      while ((b = inStream.ReadByte()) > -1)
        outStream.WriteByte( (byte) b);

      // clean up
      outStream.Flush();
      outStream.Close();
      inStream.Close();

    }

  }

}