SeekOrigin.End

Myclos81

New member
Joined
Apr 22, 2008
Messages
1
Programming Experience
1-3
Hi,

I have following code to write to a file. Normally with w.BaseStream.Seek(0, SeekOrigin.End), it should go to the end of the page and then begin writing. But it doesn't. It just overwrites everything ? How is this possible ??


Dim fs As New System.IO.FileStream("Stock Geheugensticks.doc", FileMode.Create, FileAccess.Write)
Dim w As New StreamWriter(fs)
w.BaseStream.Seek(0, SeekOrigin.End)
w.WriteLine(Date.Today & " " & FormatDateTime(Now, DateFormat.ShortTime))
w.WriteLine("Naam:")
w.WriteLine(cmb_geheugenstick.Text)
w.WriteLine("In stock:")
w.WriteLine(lbl_in_stock_geheugenstick.Text)
w.WriteLine("Uit stock:")
w.WriteLine(lbl_uit_stock_geheugenstick.Text)
w.WriteLine("Totaal:")
w.WriteLine(lbl_totaal_stock_geheugenstick.Text)
w.WriteLine("Aan gebruiker:")
w.WriteLine(cmb_gebruiker.Text)
w.Flush()
w.Close()
MessageBox.Show("Transfer succesvol uitgevoerd", "Transfer")
 
As cjard says. If you used FileMode.Append then you would not need to call Seek or specify FileAccess.Write.

I don't see why you're creating your StreamWriter in two steps like that anyway. It would be simpler to just do this:
VB.NET:
Dim w As New StreamWriter("Stock Geheugensticks.doc", True)
The second parameter specifies that the file should be appended to.
 

Latest posts

Back
Top