title: Writing bytes to a specific location in a file in vb
body: <p>I want to be able to seek to a specific byte in a file and then change that byte. I've tried a few things but nothing works.</p>
  
  <pre><code>        Const fileName As String = "Option.mco"
  
      Using writer As BinaryWriter = New BinaryWriter(File.Open(fileName, FileMode.Create))
          writer.Seek(39, SeekOrigin.Begin)
          writer.Write(1600)
      End Using
  </code></pre>
  
  <p>Using this code I can go to byte 39 but the thing is that everything before this byte turns into 00 and everything after it is deleted.</p>
  
  <p>I have also tried this:</p>
  
  <pre><code>     Dim fLocation = ("Option.mco")
      Dim fInfo As New FileInfo(fLocation)
      Dim numBytes As Long = fInfo.Length
      Dim fStream As New FileStream(fLocation, FileMode.Open, FileAccess.Read)
      Dim br As New BinaryReader(fStream)
      Dim data As Byte() = br.ReadBytes(CInt(numBytes))
      Dim fs As New FileStream(fLocation, FileMode.Open, FileAccess.Read)
  
  
      ' Show the number of bytes in the array.
      'Label1.Text = Convert.ToString(data.Length)
  
      'Dim writeStream As FileStream
      'writeStream = New FileStream(fLocation, FileMode.Create)
      'Dim writeBinay As New BinaryWriter(writeStream)
  
      Dim value As Short = 1600
      Dim value2 As Short = 900
      Dim dataArray(1600) As Byte
      fs.Seek(39, SeekOrigin.Begin)
      fs.Write(1600)
      'writeBinay.Write(value)
      fs.Seek(43, SeekOrigin.Begin)
      'writeBinay.Write(value2)
      fs.WriteByte(value2)
      'writeBinay.Close()
  
      br.Close()
  
      fStream.Close()
  </code></pre>
  
  <p>I have also tried this:</p>
  
  <pre><code>    Dim fLocation = ("Option.mco")
  
      Dim writeStream As New FileStream(fLocation, FileMode.Create)
      Dim writeBinay As New BinaryWriter(writeStream)
  
      Dim value As Short = 1600
      Dim value2 As Short = 900
      writeBinay.Seek(39, SeekOrigin.Begin)
      writeBinay.Write(value)
      writeBinay.Seek(43, SeekOrigin.Begin)
      writeBinay.Write(value2)
      writeBinay.Close()
  </code></pre>
  
  <p>This is the bytes of the file:
  <a href="http://puu.sh/3nxrd.jpg" rel="nofollow">http://puu.sh/3nxrd.jpg</a></p>