locked
Simultaneous Read/Write file access

    Question

  • Is there a way in VB 2005 to access a file for read and write access at the same time?  I need to collect data, open a file, locate its category, and write the new data right under the previous record.

     

    Earlier versions of VB had random access files.  FileOpen has replaced that in VB 2005 but the help file warns that this is only for legacy projects.  It does not say what has replaced it or how to Read and Write to a file simultaneously.  Can someone point me in the right direction?

     

    Thanks,

    David

    Thursday, November 30, 2006 2:55 PM

Answers

  • you can use filestream object

    Dim fs As New IO.FileStream(<filepath>, IO.FileMode.Open, IO.FileAccess.ReadWrite)
    fs.Read(<bytearray>, <offset>, <lenght>)
    fs.Write(<bytearray>, <offset>, <lenght>)

    Thursday, November 30, 2006 3:02 PM
  • Also want to mention that you then use System.Text.Encoding, choose an encoding (default for instance) and use the GetBytes() and GetString() methods to translate between the bytes in the stream and text.
    Thursday, November 30, 2006 3:28 PM
  • You could use a StreamReader on top of your FileStream, if it's just text, which will allow you to read lines quite conveniently.
    Thursday, November 30, 2006 5:51 PM

All replies

  • you can use filestream object

    Dim fs As New IO.FileStream(<filepath>, IO.FileMode.Open, IO.FileAccess.ReadWrite)
    fs.Read(<bytearray>, <offset>, <lenght>)
    fs.Write(<bytearray>, <offset>, <lenght>)

    Thursday, November 30, 2006 3:02 PM
  • Also want to mention that you then use System.Text.Encoding, choose an encoding (default for instance) and use the GetBytes() and GetString() methods to translate between the bytes in the stream and text.
    Thursday, November 30, 2006 3:28 PM
  • You could use a StreamReader on top of your FileStream, if it's just text, which will allow you to read lines quite conveniently.
    Thursday, November 30, 2006 5:51 PM