|
-
Feb 27th, 2010, 11:10 PM
#1
Thread Starter
Member
Using the .write and streamwriter???
-
Feb 27th, 2010, 11:19 PM
#2
Thread Starter
Member
Re: Using the .write and streamwriter???
-
Feb 27th, 2010, 11:28 PM
#3
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
-
Feb 27th, 2010, 11:30 PM
#4
Re: Using the .write and streamwriter???
 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.
-
Feb 27th, 2010, 11:48 PM
#5
Thread Starter
Member
Re: Using the .write and streamwriter???
-
Feb 28th, 2010, 12:00 AM
#6
Lively Member
Re: Using the .write and streamwriter???
Code:
My.Computer.FileSystem.WriteAllText("TheFile.txt", textbox1.text, False)
small one
-
Feb 28th, 2010, 12:05 AM
#7
Re: Using the .write and streamwriter???
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|