Link to home
Start Free TrialLog in
Avatar of gtsoulou
gtsoulou

asked on

Insert data in a file

Hi. I want to insert some data in a file. The file allready exists and has data. So I want to write in this file but not at the end of file, whithout overwritting the existing data in this possition and without using a temporary file. I tried this code but doesn't works because the new data overwrites the old:

Dim fs As FileStream = New FileStream("data.bin", FileMode.OpenOrCreate, FileAccess.Write)
        Dim w As BinaryWriter = New BinaryWriter(fs)      ' create a BinaryWriter

        w.BaseStream.Seek(4, SeekOrigin.Begin) ' seek the beginning of the file after 4bytes
        w.Write(CType(CInt(TextBox1.Text), Int32))
        fs.Flush()
        fs.Close()

The code above overwrites the data from 4th byte to 8th byte (because Int32 is 4 bytes) and writes there the new data.
 
ASKER CERTIFIED SOLUTION
Avatar of holli
holli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bob Learned
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by holli

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer