Using the .write and streamwriter???
Basically all I need is my program to write a character(Ex: "1") in a certain position specified by me in a line in a textbox. :bigyello::bigyello::bigyello:
So so far my code is:
Code:
Dim sw As IO.StreamWriter = IO.File.AppendText("101.txt")
sw.Write()
And all i need to know is how i pick the position that the .write function writes.
:S
Re: Using the .write and streamwriter???
Re: Using the .write and streamwriter???
What a StreamWriter actually does is sit on top of a Stream object. When you write text to the StreamWriter it converts it to binary, i.e. a Byte array, and then writes that to the Stream. When you're writing to a file the Stream is actually a FileStream, but it works the same way with any Stream, e.g. a NetworkStream to write text over a network connection.
The StreamWriter has no way for you to specify the position at which to write. It simply passes the binary data to the Stream and then the Stream writes to its current position. As such, you need to get that Stream from the StreamWriter's BaseStream property and then call its Seek method. Seek will move the current position a specific number of Bytes from either the present location, the start of the file or the end of the file. That means that you need to know the position at which you want to write in those terms. E.g. 1:
vb.net Code:
Using writer As New StreamWriter("file path here")
'Write the new text 100 Unicode characters from the beginning of the file.
writer.BaseStream.Seek(200L, SeekOrigin.Begin)
'Overwrite the data at the current position.
writer.Write("Hello World")
End Using
E.g. 2:
vb.net Code:
Using file As New FileStream("file path here", FileMode.Open)
'Write the new text 100 ASCII characters from the end of the file.
file.Seek(100L, SeekOrigin.End)
Using writer As New StreamWriter(file)
'Overwrite the data at the current position.
writer.Write("Hello World")
End Using
End Using
Re: Using the .write and streamwriter???
Quote:
Originally Posted by
NewbieProgramist
Unresolved
Please don't bump your thread 9 minutes after creating it. Bumping threads at all is against forum guidelines. Everyone here is a volunteer so we will get to your question if and when we can. If your thread slips off the first page without an answer, then you need to consider why that might be and try to provide more information, clarify the problem or at least ask what people might need in order to help.
Re: Using the .write and streamwriter???
Re: Using the .write and streamwriter???
Code:
My.Computer.FileSystem.WriteAllText("TheFile.txt", textbox1.text, False)
small one
Re: Using the .write and streamwriter???
Quote:
Originally Posted by
watson123
Code:
My.Computer.FileSystem.WriteAllText("TheFile.txt", textbox1.text, False)
small one
That will overwrite the entire contents of the file with the contents of the TextBox, which is not what was asked for in this case.