Use FileStream to write/read a file byte by byte : FileStream : File Directory Stream C# Examples


C# Examples » File Directory Stream » FileStream »

 

Use FileStream to write/read a file byte by byte









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

class  MainClass
{
    public  static  void  Main()  
    {
        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;

            while  ((b  =  inStream.ReadByte())  >  -1)
                outStream.WriteByte(  (byte)  b);

            outStream.Flush();
            outStream.Close();
            inStream.Close();
        }
    }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo File Directory Stream
» FileStream