title: Open/Read a binary file - access rights
body: <p>I am trying to convert VB5 to .NET and cannot get a binary read to work. My VB.NET decode only reads the first two characters correctly. 
  The (VB5->VB.NET) encode is</p>
  
  <pre><code>    ' Open file
      x = Rnd(-mKeyValue)
      filenum = FreeFile()
      Try
          FileOpen(filenum, Filename, OpenMode.Binary)
      Catch ex As IO.IOException
          MsgBox(ex.ToString, MsgBoxStyle.Critical, "File opening error")
          Exit Sub
      End Try
      ' write data
      filecontents = ""       
  
      For i = 1 To Len(stringdate)
          charnum = Asc(Mid(stringdate, i, 1))
          randomint = Int(256 * Rnd())
          charnum = charnum Xor randomint
          singlechar = Chr(charnum)
          FilePut(filenum, singlechar, i)
          filecontents = filecontents &amp; singlechar
      Next i
  </code></pre>
  
  <p>And the (VB5->VB.NET) decode is</p>
  
  <pre><code>    x = Rnd(-mKeyValue)
      filenum = FreeFile()
      FileOpen(filenum, Filename, OpenMode.Binary)
      For i = 1 To LOF(filenum)
  
          'To VB.NET
          FileGet(filenum, singlechar, i)
          charnum = Asc(singlechar)
          Debug.Print("VB5 singlechar = " &amp; singlechar)
          randomint = Int(256 * Rnd())
          charnum = charnum Xor randomint
          singlechar = Chr(charnum)
       Next i
  </code></pre>
  
  <p>My VB.NET code which fails (cannot read the file correctly) is;</p>
  
  <pre><code>      Using reader As New BinaryReader(File.Open(Filename, FileMode.Open))
          ' Loop through length of file.
          Dim pos As Integer = 0
          Dim length As Integer = reader.BaseStream.Length
  
  
          While pos &lt; length
              ' Read the integer.
              singlechar = reader.ReadChar()
              charnum = Asc(singlechar)    'singlechar is type Char
              randomint = Int(256 * Rnd())
              charnum = charnum Xor randomint
              singlechar = Chr(charnum)
  
              i += 1
          End While
      End Using
  </code></pre>
  
  <p>Can anyone help me with translation from VB5 to .NET?</p>