title: Read/Write string binary data - BinaryReader
body: <p>I wrote the below method to archive files into one file using binary mode:</p>
  
  <pre><code>        // Compile archive
          public void CompileArchive(string FilePath, ListView FilesList, Label Status, ProgressBar Progress)
          {
              FileTemplate TempFile = new FileTemplate();
              if (FilesList.Items.Count &gt; 0)
              {
                  BinaryWriter Writer = new BinaryWriter(File.Open(FilePath, FileMode.Create), System.Text.Encoding.ASCII);
                  Progress.Maximum = FilesList.Items.Count - 1;
                  Writer.Write((long)FilesList.Items.Count);
                  for (int i = 0; i &lt;= FilesList.Items.Count - 1; i++)
                  {
                      TempFile.Name = FilesList.Items[i].SubItems[1].Text;
                      TempFile.Path = "%ARCHIVE%";
                      TempFile.Data = this.ReadFileData(FilesList.Items[i].SubItems[2].Text + "\\" + TempFile.Name);
                      Writer.Write(TempFile.Name);
                      Writer.Write(TempFile.Path);
                      Writer.Write(TempFile.Data);
                      Status.Text = "Status: Writing '" + TempFile.Name + "'";
                      Progress.Value = i;
                  }
                  Writer.Close();
                  Status.Text = "Status: None";
                  Progress.Value = 0;
              }
          }
  </code></pre>
  
  <p>I read files data using ReadFileData which is in the above method method which return a string of data. (StreamReader) Next up I extract my archive. Everything is done great but the data which will being stored in the extraction method doesn't give me a right data so the extracted files have not right data to show their original functionality.</p>
  
  <p>Extract method:</p>
  
  <pre><code>    // Extract archive
      public void ExtractArchive(string ArchivePath, string ExtractPath, ListView FilesList, Label Status, ProgressBar Progress)
      {
          FileTemplate TempFile = new FileTemplate();
          BinaryReader Reader = new BinaryReader(File.Open(ArchivePath, FileMode.Open), System.Text.Encoding.ASCII);
          long Count = Reader.ReadInt64();
          if (Count &gt; 0)
          {
              Progress.Maximum = (int)Count - 1;
              FilesList.Items.Clear();
              for (int i = 0; i &lt;= Count - 1; i++)
              {
                  TempFile.Name = Reader.ReadString();
                  TempFile.Path = Reader.ReadString();
                  TempFile.Data = Reader.ReadString();
                  Status.Text = "Status: Reading '" + TempFile.Name + "'";
                  Progress.Value = i;
                  if (!Directory.Exists(ExtractPath))
                  {
                      Directory.CreateDirectory(ExtractPath);
                  }
                  BinaryWriter Writer = new BinaryWriter(File.Open(ExtractPath + "\\" + TempFile.Name, FileMode.Create), System.Text.Encoding.ASCII);
                  Writer.Write(TempFile.Data);
                  Writer.Close();
                  string[] ItemArr = new string[] { i.ToString(), TempFile.Name, TempFile.Path };
                  ListViewItem ListItem = new ListViewItem(ItemArr);
                  FilesList.Items.Add(ListItem);
              }
              Reader.Close();
              Status.Text = "Status: None";
              Progress.Value = 0;
          }
      }
  </code></pre>
  
  <p>The structure:</p>
  
  <pre><code>struct FileTemplate
          {
              public string Name, Path, Data;
          }
  </code></pre>
  
  <p>Thanks.</p>