PS:调试的时候在“命令行参数”属性中,输入“..\..\Test.txt”,便可以将与源代码(indexer.cs)所在目录下的Test.txt文件中的字节反转。或者将编译好的exe在cmd中输入“indexer Test.txt”也可。
这个范例也提供了按照字节访问文件的方法(类FileByteArray),收藏收藏了, 
Demo
 //Copyright (C) Microsoft Corporation.  All rights reserved.
//Copyright (C) Microsoft Corporation.  All rights reserved.
 // indexer.cs
// indexer.cs // arguments: indexer.txt
// arguments: indexer.txt using System;
using System; using System.IO;
using System.IO;
 // Class to provide access to a large file
// Class to provide access to a large file // as if it were a byte array.
// as if it were a byte array. public class FileByteArray
public class FileByteArray

 {
{ Stream stream;      // Holds the underlying stream
    Stream stream;      // Holds the underlying stream // used to access the file.
                        // used to access the file. // Create a new FileByteArray encapsulating a particular file.
// Create a new FileByteArray encapsulating a particular file. public FileByteArray(string fileName)
    public FileByteArray(string fileName)
 
     {
{ stream = new FileStream(fileName, FileMode.Open);
        stream = new FileStream(fileName, FileMode.Open); }
    }
 // Close the stream. This should be the last thing done
    // Close the stream. This should be the last thing done // when you are finished.
    // when you are finished. public void Close()
    public void Close()
 
     {
{ stream.Close();
        stream.Close(); stream = null;
        stream = null; }
    }
 // Indexer to provide read/write access to the file.
    // Indexer to provide read/write access to the file. public byte this[long index]   // long is a 64-bit integer
    public byte this[long index]   // long is a 64-bit integer
 
     {
{ // Read one byte at offset index and return it.
        // Read one byte at offset index and return it. get
        get 
 
         {
{ byte[] buffer = new byte[1];
            byte[] buffer = new byte[1]; stream.Seek(index, SeekOrigin.Begin);
            stream.Seek(index, SeekOrigin.Begin); stream.Read(buffer, 0, 1);
            stream.Read(buffer, 0, 1); return buffer[0];
            return buffer[0]; }
        } // Write one byte at offset index and return it.
        // Write one byte at offset index and return it. set
        set 
 
         {
{
 byte[] buffer = new byte[1]
            byte[] buffer = new byte[1]  {value};
{value}; stream.Seek(index, SeekOrigin.Begin);
            stream.Seek(index, SeekOrigin.Begin); stream.Write(buffer, 0, 1);
            stream.Write(buffer, 0, 1); }
        } }
    }
 // Get the total length of the file.
    // Get the total length of the file. public long Length
    public long Length 
 
     {
{ get
        get 
 
         {
{ return stream.Seek(0, SeekOrigin.End);
            return stream.Seek(0, SeekOrigin.End); }
        } }
    } }
}
 // Demonstrate the FileByteArray class.
// Demonstrate the FileByteArray class. // Reverses the bytes in a file.
// Reverses the bytes in a file. public class Reverse
public class Reverse 

 {
{ public static void Main(String[] args)
    public static void Main(String[] args) 
 
     {
{ // Check for arguments.
        // Check for arguments. if (args.Length != 1)
        if (args.Length != 1)
 
         {
{ Console.WriteLine(“Usage : Indexer <filename>“);
            Console.WriteLine(“Usage : Indexer <filename>“); return;
            return; }
        }
 // Check for file existence
        // Check for file existence if (!System.IO.File.Exists(args[0]))
        if (!System.IO.File.Exists(args[0]))
 
         {
{ Console.WriteLine(“File “ + args[0] + “ not found.“);
            Console.WriteLine(“File “ + args[0] + “ not found.“); return;
            return; }
        }
 FileByteArray file = new FileByteArray(args[0]);
        FileByteArray file = new FileByteArray(args[0]); long len = file.Length;
        long len = file.Length;
 // Swap bytes in the file to reverse it.
        // Swap bytes in the file to reverse it. for (long i = 0; i < len / 2; ++i)
        for (long i = 0; i < len / 2; ++i) 
 
         {
{ byte t;
            byte t;
 // Note that indexing the “file” variable invokes the
            // Note that indexing the “file” variable invokes the // indexer on the FileByteStream class, which reads
            // indexer on the FileByteStream class, which reads // and writes the bytes in the file.
            // and writes the bytes in the file. t = file[i];
            t = file[i]; file[i] = file[len – i – 1];
            file[i] = file[len – i – 1]; file[len – i – 1] = t;
            file[len – i – 1] = t; }
        }
 file.Close();
        file.Close(); }
    }  }
}

