Results 1 to 7 of 7

Thread: Using the .write and streamwriter???

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    50

    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.

    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

  2. #2

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    50

    Re: Using the .write and streamwriter???

    Unresolved

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Using writer As New StreamWriter("file path here")
    2.     'Write the new text 100 Unicode characters from the beginning of the file.
    3.     writer.BaseStream.Seek(200L, SeekOrigin.Begin)
    4.  
    5.     'Overwrite the data at the current position.
    6.     writer.Write("Hello World")
    7. End Using
    E.g. 2:
    vb.net Code:
    1. Using file As New FileStream("file path here", FileMode.Open)
    2.     'Write the new text 100 ASCII characters from the end of the file.
    3.     file.Seek(100L, SeekOrigin.End)
    4.  
    5.     Using writer As New StreamWriter(file)
    6.         'Overwrite the data at the current position.
    7.         writer.Write("Hello World")
    8.     End Using
    9. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Using the .write and streamwriter???

    Quote Originally Posted by NewbieProgramist View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    50

    Re: Using the .write and streamwriter???

    Resolved thanks PMR

  6. #6
    Lively Member
    Join Date
    Feb 2010
    Posts
    120

    Re: Using the .write and streamwriter???

    Code:
    My.Computer.FileSystem.WriteAllText("TheFile.txt", textbox1.text, False)
    small one

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Using the .write and streamwriter???

    Quote Originally Posted by watson123 View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width