that is, using the streamWriter class
I'm opening a textfile file File.AppenText("blah") function. But the append mode adds everything to the end of the file. I want to move to the begining of the file. Is there a seek function?
that is, using the streamWriter class
I'm opening a textfile file File.AppenText("blah") function. But the append mode adds everything to the end of the file. I want to move to the begining of the file. Is there a seek function?
Use File.OpenText and the WriteLine method of the StreamWriter class. :)
interesting interesting:D thanksQuote:
Originally posted by Sheppe
Use File.OpenText and the WriteLine method of the StreamWriter class. :)
umm ahem but OpenText returns a streamReader object, and OpenWrite returns fileStream:confused:
Whoops! :o
Try this:
VB Code:
' Create an instance of StreamWriter to write text to a file. Dim sw As StreamWriter = New StreamWriter("TestFile.txt") ' Add some text to the file. sw.Write("This is the ") sw.WriteLine("header for the file.") sw.WriteLine("-------------------") ' Arbitrary objects can also be written to the file. sw.Write("The date is: ") sw.WriteLine(DateTime.Now) sw.Close()
hihi, but that doesnt APPEND to the file :( that clears eveyrthing in the file and writes the new content. Now I could read the whole file before I write any new data to it, but that kinda sounds stupid....
there is a SEEK() function but it only works with the old form vb functions (open/close) and I dont really wanna use them.... any way to append to the begining of the file? hmm I thought it's really simple
thanks for the replies btw :)
Isn't there a way to merge two text files? If there is then you write your new line to a new file then merge it with the other file. Will give it a try and let you know.
hmm there is streamwriter.BaseStream.Seek() how do I use it
OMG I cant believe I'm turning this dumb... can't figure out the simplest thing in the universe, haha
anyways wasnt there a method that would create a file if it didnt exist, or it would open it if it already exitsted? My way seems kinda akward:
VB Code:
Dim sw As StreamWriter If File.Exists(journalPath) Then sw = File.AppendText(journalPath) Else sw = File.CreateText(journalPath) End If
Really there is no need to find the first line of the file because when you open the file or stream you are at the first line. Although you can change the position through the Position property of the FileStream, for instance returning to the start you'd use fs.Position=0. But there is a problem with that because if you just start writing at that position then you will write over whatever is already there. The only way to append text to the front if a file is read the contents to a variable then move to the front, write the new content line and then rewrite the old content after it. Also you can use the FileMode.OpenOrCreate in making a FileStream to have it automatically open an existing file or create one if there is none yet. So here is a quick function to append text to the front of a file:
VB Code:
Public Sub AppendToFront(ByVal filename As String, ByVal text As String) 'open the fiel for read.write Dim fs As New IO.FileStream(filename, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite) Dim sr As New IO.StreamReader(fs) 'read old data Dim oldText As String = sr.ReadToEnd Dim sw As New IO.StreamWriter(fs) 'move to start of file fs.Position = 0 'write new text in front sw.WriteLine(text) 'write old text after sw.WriteLine(oldText) 'flushes writer to file sw.Flush() 'clean up fs.Close() End Sub 'syntax AppendToFront("..\sample.txt", TextBox2.Text)
aah hehe thanks Edneeis. One question though, is it necessary to call the Flush function? Couldnt you just close the stream?
Probably, try and see. Since I used two Stream Helpers I didn't want one to feel left out if I didn't use it so I closed the filestream instead. I'm pretty sure the StreamWriter/Reader will flush when closed though. Its all basically the samething.