<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FFile_Directory_Stream%2FFileStream</id>
		<title>Csharp/CSharp Tutorial/File Directory Stream/FileStream - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FFile_Directory_Stream%2FFileStream"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/File_Directory_Stream/FileStream&amp;action=history"/>
		<updated>2026-04-29T15:01:48Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/File_Directory_Stream/FileStream&amp;diff=6798&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/File_Directory_Stream/FileStream&amp;diff=6798&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:53Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/File_Directory_Stream/FileStream&amp;diff=6799&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/File_Directory_Stream/FileStream&amp;diff=6799&amp;oldid=prev"/>
				<updated>2010-05-26T12:20:27Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Binary File Reading through FileStream==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.ruponentModel;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Drawing;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
using System.IO;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        int nCols = 16;&lt;br /&gt;
        FileStream inStream = new FileStream(&amp;quot;c:\\a.txt&amp;quot;, FileMode.Open, FileAccess.Read);&lt;br /&gt;
        long nBytesToRead = inStream.Length;&lt;br /&gt;
        if (nBytesToRead &amp;gt; 65536 / 4)&lt;br /&gt;
            nBytesToRead = 65536 / 4;&lt;br /&gt;
        int nLines = (int)(nBytesToRead / nCols) + 1;&lt;br /&gt;
        string[] lines = new string[nLines];&lt;br /&gt;
        int nBytesRead = 0;&lt;br /&gt;
        for (int i = 0; i &amp;lt; nLines; i++) {&lt;br /&gt;
            StringBuilder nextLine = new StringBuilder();&lt;br /&gt;
            nextLine.Capacity = 4 * nCols;&lt;br /&gt;
            for (int j = 0; j &amp;lt; nCols; j++) {&lt;br /&gt;
                int nextByte = inStream.ReadByte();&lt;br /&gt;
                nBytesRead++;&lt;br /&gt;
                if (nextByte &amp;lt; 0 || nBytesRead &amp;gt; 65536)&lt;br /&gt;
                    break;&lt;br /&gt;
                char nextChar = (char)nextByte;&lt;br /&gt;
                if (nextChar &amp;lt; 16)&lt;br /&gt;
                    nextLine.Append(&amp;quot; x0&amp;quot; + string.Format(&amp;quot;{0,1:X}&amp;quot;, (int)nextChar));&lt;br /&gt;
                else if&lt;br /&gt;
                    (char.IsLetterOrDigit(nextChar) || char.IsPunctuation(nextChar))&lt;br /&gt;
                    nextLine.Append(&amp;quot;  &amp;quot; + nextChar + &amp;quot; &amp;quot;);&lt;br /&gt;
                else&lt;br /&gt;
                    nextLine.Append(&amp;quot; x&amp;quot; + string.Format(&amp;quot;{0,2:X}&amp;quot;, (int)nextChar));&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        inStream.Close();&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Demonstrate random access file==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System; &lt;br /&gt;
using System.IO;  &lt;br /&gt;
 &lt;br /&gt;
class MainClass { &lt;br /&gt;
  public static void Main() { &lt;br /&gt;
    FileStream f; &lt;br /&gt;
    char ch; &lt;br /&gt;
 &lt;br /&gt;
    try { &lt;br /&gt;
      f = new FileStream(&amp;quot;random.dat&amp;quot;, FileMode.Create); &lt;br /&gt;
    } &lt;br /&gt;
    catch(IOException exc) { &lt;br /&gt;
      Console.WriteLine(exc.Message); &lt;br /&gt;
      return ; &lt;br /&gt;
    } &lt;br /&gt;
 &lt;br /&gt;
    // Write the alphabet.      &lt;br /&gt;
    for(int i=0; i &amp;lt; 26; i++) { &lt;br /&gt;
      try { &lt;br /&gt;
        f.WriteByte((byte)(&amp;quot;A&amp;quot;+i)); &lt;br /&gt;
      }  &lt;br /&gt;
      catch(IOException exc) { &lt;br /&gt;
        Console.WriteLine(exc.Message); &lt;br /&gt;
        return ; &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
 &lt;br /&gt;
    try { &lt;br /&gt;
      f.Seek(0, SeekOrigin.Begin); // seek to first byte &lt;br /&gt;
      ch = (char) f.ReadByte(); &lt;br /&gt;
      Console.WriteLine(&amp;quot;First value is &amp;quot; + ch); &lt;br /&gt;
 &lt;br /&gt;
      f.Seek(1, SeekOrigin.Begin); // seek to second byte &lt;br /&gt;
      ch = (char) f.ReadByte(); &lt;br /&gt;
      Console.WriteLine(&amp;quot;Second value is &amp;quot; + ch); &lt;br /&gt;
 &lt;br /&gt;
      f.Seek(4, SeekOrigin.Begin); // seek to 5th byte &lt;br /&gt;
      ch = (char) f.ReadByte(); &lt;br /&gt;
      Console.WriteLine(&amp;quot;Fifth value is &amp;quot; + ch); &lt;br /&gt;
 &lt;br /&gt;
      Console.WriteLine(); &lt;br /&gt;
 &lt;br /&gt;
    }  &lt;br /&gt;
    catch(IOException exc) { &lt;br /&gt;
      Console.WriteLine(exc.Message); &lt;br /&gt;
    } &lt;br /&gt;
  &lt;br /&gt;
    Console.WriteLine(); &lt;br /&gt;
    f.Close(); &lt;br /&gt;
  } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;First value is A&lt;br /&gt;
Second value is B&lt;br /&gt;
Fifth value is E&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Is a stream seekable==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        byte[] buf2 = new byte[] { 97, 112, 112, 108, 101, 115, 97, 117, 99, 101 };&lt;br /&gt;
        FileStream s = new FileStream(&amp;quot;Foo.txt&amp;quot;, FileMode.Open);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Length: {0}, Position: {1}&amp;quot;, s.Length, s.Position);&lt;br /&gt;
        if (s.CanSeek) {&lt;br /&gt;
            s.Seek(13, SeekOrigin.Begin);&lt;br /&gt;
            Console.WriteLine(&lt;br /&gt;
                &amp;quot;Position: {0}&amp;quot;, s.Position);&lt;br /&gt;
            s.Write(buf2, 0, buf2.Length);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Move file pointer to beginning of file==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.IO;&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            FileStream aFile = new FileStream(&amp;quot;Temp.txt&amp;quot;, FileMode.Create);&lt;br /&gt;
            char[] charData = &amp;quot;this is a test.&amp;quot;.ToCharArray();&lt;br /&gt;
            byte[] byData = new byte[charData.Length];&lt;br /&gt;
            Encoder e = Encoding.UTF8.GetEncoder();&lt;br /&gt;
            e.GetBytes(charData, 0, charData.Length, byData, 0, true);&lt;br /&gt;
            // Move file pointer to beginning of file.&lt;br /&gt;
            aFile.Seek(0, SeekOrigin.Begin);&lt;br /&gt;
            aFile.Write(byData, 0, byData.Length);&lt;br /&gt;
        }&lt;br /&gt;
    }&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Open a file with exception handling==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;FileStream fin; &lt;br /&gt;
 &lt;br /&gt;
try { &lt;br /&gt;
  fin = new FileStream(&amp;quot;test.dat&amp;quot;, FileMode.Open); &lt;br /&gt;
} &lt;br /&gt;
catch(FileNotFoundException exc) { &lt;br /&gt;
  Console.WriteLine(exc.Message); &lt;br /&gt;
  return; &lt;br /&gt;
} &lt;br /&gt;
catch { &lt;br /&gt;
  Console.WriteLine(&amp;quot;Cannot open file.&amp;quot;); &lt;br /&gt;
  return; &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Open an existing file==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
  static void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    FileInfo MyFile = new FileInfo(@&amp;quot;c:\Testing.txt&amp;quot;);&lt;br /&gt;
    FileStream MyStream;&lt;br /&gt;
    MyStream = MyFile.Open(FileMode.Open, FileAccess.Read, FileShare.None);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Read every other value using FileSeek==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System; &lt;br /&gt;
using System.IO;  &lt;br /&gt;
 &lt;br /&gt;
class MainClass { &lt;br /&gt;
  public static void Main() { &lt;br /&gt;
    FileStream f; &lt;br /&gt;
    char ch; &lt;br /&gt;
 &lt;br /&gt;
    try { &lt;br /&gt;
      f = new FileStream(&amp;quot;random.dat&amp;quot;, FileMode.Create); &lt;br /&gt;
    } &lt;br /&gt;
    catch(IOException exc) { &lt;br /&gt;
      Console.WriteLine(exc.Message); &lt;br /&gt;
      return ; &lt;br /&gt;
    } &lt;br /&gt;
 &lt;br /&gt;
    // Write the alphabet.      &lt;br /&gt;
    for(int i=0; i &amp;lt; 26; i++) { &lt;br /&gt;
      try { &lt;br /&gt;
        f.WriteByte((byte)(&amp;quot;A&amp;quot;+i)); &lt;br /&gt;
      }  &lt;br /&gt;
      catch(IOException exc) { &lt;br /&gt;
        Console.WriteLine(exc.Message); &lt;br /&gt;
        return ; &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
 &lt;br /&gt;
    try { &lt;br /&gt;
      Console.WriteLine(&amp;quot;Here is every other value: &amp;quot;); &lt;br /&gt;
      for(int i=0; i &amp;lt; 26; i += 2) { &lt;br /&gt;
        f.Seek(i, SeekOrigin.Begin); // seek to ith double &lt;br /&gt;
        ch = (char) f.ReadByte(); &lt;br /&gt;
        Console.Write(ch + &amp;quot; &amp;quot;); &lt;br /&gt;
      } &lt;br /&gt;
 &lt;br /&gt;
    }  &lt;br /&gt;
    catch(IOException exc) { &lt;br /&gt;
      Console.WriteLine(exc.Message); &lt;br /&gt;
    } &lt;br /&gt;
  &lt;br /&gt;
    Console.WriteLine(); &lt;br /&gt;
    f.Close(); &lt;br /&gt;
  } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Here is every other value:&lt;br /&gt;
A C E G I K M O Q S U W Y&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Read file stream as an array of bytes==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
  static void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    FileStream MyFileStream1 = new FileStream(@&amp;quot;c:\Testing.txt&amp;quot;, FileMode.Create);&lt;br /&gt;
    int NumberOfBytes = 200;&lt;br /&gt;
    byte[] MyByteArray = new Byte[NumberOfBytes];&lt;br /&gt;
    int BytesRead = MyFileStream1.Read(MyByteArray, 0, NumberOfBytes);&lt;br /&gt;
    &lt;br /&gt;
    MyFileStream1.Close();    &lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Read file stream on a per byte basis==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
  static void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    FileStream MyFileStream1 = new FileStream(@&amp;quot;c:\Testing.txt&amp;quot;, FileMode.Create);&lt;br /&gt;
    &lt;br /&gt;
    int MyBytes = MyFileStream1.ReadByte();&lt;br /&gt;
    &lt;br /&gt;
    MyFileStream1.Close();    &lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Reset internal position for a FileStream==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static int Main(string[] args)&lt;br /&gt;
    {      &lt;br /&gt;
    FileInfo f = new FileInfo(@&amp;quot;C:\test.txt&amp;quot;);&lt;br /&gt;
    FileStream  s = f.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);    &lt;br /&gt;
    for(int i = 0; i &amp;lt; 256; i++)&lt;br /&gt;
    {&lt;br /&gt;
      s.WriteByte((byte)i);&lt;br /&gt;
    }&lt;br /&gt;
    // Reset internal position.&lt;br /&gt;
    s.Position = 0;&lt;br /&gt;
    for(int i = 0; i &amp;lt; 256; i++)&lt;br /&gt;
    {&lt;br /&gt;
      Console.Write(s.ReadByte());  &lt;br /&gt;
    }&lt;br /&gt;
    s.Close();&lt;br /&gt;
    f.Delete();&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Text File Reading with FileStream==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.IO;&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] byData = new byte[200];&lt;br /&gt;
            char[] charData = new Char[200];&lt;br /&gt;
            FileStream aFile = new FileStream(&amp;quot;Program.cs&amp;quot;, FileMode.Open);&lt;br /&gt;
            aFile.Seek(113, SeekOrigin.Begin);&lt;br /&gt;
            aFile.Read(byData, 0, 200);&lt;br /&gt;
            Decoder d = Encoding.UTF8.GetDecoder();&lt;br /&gt;
            d.GetChars(byData, 0, byData.Length, charData, 0);&lt;br /&gt;
            Console.WriteLine(charData);&lt;br /&gt;
        }&lt;br /&gt;
    }&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==The FileMode and FileAccess==&lt;br /&gt;
&lt;br /&gt;
Value&lt;br /&gt;
Description&lt;br /&gt;
FileMode.Append&lt;br /&gt;
append to the end of file.&lt;br /&gt;
FileMode.Create&lt;br /&gt;
create a new output file. Any preexisting file by the same name will be destroyed.&lt;br /&gt;
FileMode.CreateNew&lt;br /&gt;
create a new output file. The file must not already exist.&lt;br /&gt;
FileMode.Open&lt;br /&gt;
open a preexisting file.&lt;br /&gt;
FileMode.OpenOrCreate&lt;br /&gt;
open a file if it exists, or create the file if it does not already exist.&lt;br /&gt;
FileMode.Truncate&lt;br /&gt;
open a preexisting file, but reduce its length to zero.&lt;br /&gt;
&amp;lt;p&amp;gt;If you want to restrict access to just reading or just writing, use this constructor:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;FileStream(string filename, FileMode mode, FileAccess how)&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use FileStream to read a file byte by byte==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
using System.IO;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  public static void Main() &lt;br /&gt;
  {&lt;br /&gt;
    OpenFileDialog dlgOpen = new OpenFileDialog();&lt;br /&gt;
    dlgOpen.Title=&amp;quot;Select file to back up&amp;quot;;&lt;br /&gt;
    if (dlgOpen.ShowDialog() == DialogResult.OK)&lt;br /&gt;
    {&lt;br /&gt;
      FileStream inStream = File.OpenRead(dlgOpen.FileName);&lt;br /&gt;
      FileStream outStream =  File.OpenWrite(dlgOpen.FileName + &amp;quot;.bak&amp;quot;);&lt;br /&gt;
      int b;&lt;br /&gt;
      while ((b = inStream.ReadByte()) &amp;gt; -1)&lt;br /&gt;
        outStream.WriteByte( (byte) b);&lt;br /&gt;
      outStream.Flush();&lt;br /&gt;
      outStream.Close();&lt;br /&gt;
      inStream.Close();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use FileStream to read a file selected from OpenFileDialog==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
using System.IO;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  public static void Main() &lt;br /&gt;
  {&lt;br /&gt;
    OpenFileDialog dlgOpen = new OpenFileDialog();&lt;br /&gt;
    dlgOpen.Title=&amp;quot;Select file to back up&amp;quot;;&lt;br /&gt;
    if (dlgOpen.ShowDialog() == DialogResult.OK)&lt;br /&gt;
    {&lt;br /&gt;
      FileStream inStream = File.OpenRead(dlgOpen.FileName);&lt;br /&gt;
      FileStream outStream =  File.OpenWrite(dlgOpen.FileName + &amp;quot;.bak&amp;quot;);&lt;br /&gt;
      int b;&lt;br /&gt;
      while ((b = inStream.ReadByte()) &amp;gt; -1)&lt;br /&gt;
        outStream.WriteByte( (byte) b);&lt;br /&gt;
      outStream.Flush();&lt;br /&gt;
      outStream.Close();&lt;br /&gt;
      inStream.Close();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use FileStream to write/read a file byte by byte==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
using System.IO;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  public static void Main() &lt;br /&gt;
  {&lt;br /&gt;
    OpenFileDialog dlgOpen = new OpenFileDialog();&lt;br /&gt;
    dlgOpen.Title=&amp;quot;Select file to back up&amp;quot;;&lt;br /&gt;
    if (dlgOpen.ShowDialog() == DialogResult.OK)&lt;br /&gt;
    {&lt;br /&gt;
      FileStream inStream = File.OpenRead(dlgOpen.FileName);&lt;br /&gt;
      FileStream outStream =  File.OpenWrite(dlgOpen.FileName + &amp;quot;.bak&amp;quot;);&lt;br /&gt;
      int b;&lt;br /&gt;
      while ((b = inStream.ReadByte()) &amp;gt; -1)&lt;br /&gt;
        outStream.WriteByte( (byte) b);&lt;br /&gt;
      outStream.Flush();&lt;br /&gt;
      outStream.Close();&lt;br /&gt;
      inStream.Close();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==using statement and FileStream==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.IO;&lt;br /&gt;
  class Program&lt;br /&gt;
  {&lt;br /&gt;
    static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
      using (FileStream fStream = File.Open(@&amp;quot;C:\myMessage.dat&amp;quot;,FileMode.Create))&lt;br /&gt;
      {&lt;br /&gt;
        string msg = &amp;quot;Hello!&amp;quot;;&lt;br /&gt;
        byte[] msgAsByteArray = Encoding.Default.GetBytes(msg);&lt;br /&gt;
        fStream.Write(msgAsByteArray, 0, msgAsByteArray.Length);&lt;br /&gt;
        fStream.Position = 0;&lt;br /&gt;
        byte[] bytesFromFile = new byte[msgAsByteArray.Length];&lt;br /&gt;
        for (int i = 0; i &amp;lt; msgAsByteArray.Length; i++)&lt;br /&gt;
        {&lt;br /&gt;
          bytesFromFile[i] = (byte)fStream.ReadByte();&lt;br /&gt;
          Console.Write(bytesFromFile[i]);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(Encoding.Default.GetString(bytesFromFile));&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Write byte array with FileStream==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.IO;&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            FileStream aFile = new FileStream(&amp;quot;Temp.txt&amp;quot;, FileMode.Create);&lt;br /&gt;
            char[] charData = &amp;quot;this is a test.&amp;quot;.ToCharArray();&lt;br /&gt;
            byte[] byData = new byte[charData.Length];&lt;br /&gt;
            Encoder e = Encoding.UTF8.GetEncoder();&lt;br /&gt;
            e.GetBytes(charData, 0, charData.Length, byData, 0, true);&lt;br /&gt;
            // Move file pointer to beginning of file.&lt;br /&gt;
            aFile.Seek(0, SeekOrigin.Begin);&lt;br /&gt;
            aFile.Write(byData, 0, byData.Length);&lt;br /&gt;
        }&lt;br /&gt;
    }&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Write data to file through FileStream per byte==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
  static void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    FileStream MyFileStream1 = new FileStream(@&amp;quot;c:\Testing.txt&amp;quot;, FileMode.Create);&lt;br /&gt;
    byte MyWriteByte = 100;&lt;br /&gt;
    MyFileStream1.WriteByte(MyWriteByte);&lt;br /&gt;
    &lt;br /&gt;
    MyFileStream1.Close();    &lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Write data to file through FileStream via an array==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
  static void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    FileStream MyFileStream1 = new FileStream(@&amp;quot;c:\Testing.txt&amp;quot;, FileMode.Create);&lt;br /&gt;
    int NumberOfBytesToWrite = 256;&lt;br /&gt;
    byte[] MyWriteByteArray = new Byte[NumberOfBytesToWrite];&lt;br /&gt;
    for (int i = 0; i &amp;lt; 256; i++)&lt;br /&gt;
    {&lt;br /&gt;
      MyWriteByteArray[i] = (byte)i;&lt;br /&gt;
      i++;&lt;br /&gt;
    }&lt;br /&gt;
    MyFileStream1.Write(MyWriteByteArray, 0, NumberOfBytesToWrite);&lt;br /&gt;
    &lt;br /&gt;
    MyFileStream1.Close();    &lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Write/read bytes using FileStream==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static int Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
    FileStream myFStream = new FileStream(&amp;quot;test.dat&amp;quot;, FileMode.OpenOrCreate, FileAccess.ReadWrite);&lt;br /&gt;
      &lt;br /&gt;
    for(int i = 0; i &amp;lt; 256; i++)&lt;br /&gt;
      myFStream.WriteByte((byte)i);&lt;br /&gt;
    myFStream.Position = 0;&lt;br /&gt;
    for(int i = 0; i &amp;lt; 256; i++)&lt;br /&gt;
      Console.Write(myFStream.ReadByte());  &lt;br /&gt;
    Console.WriteLine();&lt;br /&gt;
    myFStream.Close();&lt;br /&gt;
    return 0;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354&lt;br /&gt;
5556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021&lt;br /&gt;
0310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513&lt;br /&gt;
6137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169&lt;br /&gt;
1701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022&lt;br /&gt;
0320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523&lt;br /&gt;
6237238239240241242243244245246247248249250251252253254255&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>