Mega Code Archive

 
Categories / C# / File Stream
 

Reads a block of bytes from the stream and writes the data in a given buffer

using System; using System.IO; class Test {     public static void Main()     {         string pathSource = @"c:\source.txt";         string pathNew = @"c:\newfile.txt";         using (FileStream fsSource = new FileStream(pathSource, FileMode.Open, FileAccess.Read))         {             byte[] bytes = new byte[fsSource.Length];             int numBytesToRead = (int)fsSource.Length;             int numBytesRead = 0;             while (numBytesToRead > 0)             {                 int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);                 if (n == 0)                     break;                 numBytesRead += n;                 numBytesToRead -= n;             }             numBytesToRead = bytes.Length;             using (FileStream fsNew = new FileStream(pathNew, FileMode.Create, FileAccess.Write))             {                 fsNew.Write(bytes, 0, numBytesToRead);             }         }     } }