Results 1 to 3 of 3

Thread: VB.net Append text in the end of txt file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    VB.net Append text in the end of txt file

    Hello, can someone advice me how i can make correct to add new value string in text file at specific location.
    I will give full example

    So let's start i have a file File1.txt
    Inside i have this information:

    Line1: /grant Username2OI)(CI)F /T
    Line2: /GRANT:Username2,Full /UNLIMITED

    so what i want to do programatically, in Line 1 i want to add after /T new record string
    example result will be:
    Code:
    /grant Username2:(OI)(CI)F /T /grant Username3:(OI)(CI)F /T
    then for line 2 i want to add before the /UNLIMITED new string
    example result will be:
    /GRANT:Username2,Full /GRANT:Username3,Full /UNLIMITED
    how i can make it like that? - Please note Username are just example text it can be other text
    And for example how can i add new line in the end with string after line2, to add new strings each line if i need?
    Last edited by luckydead; Oct 27th, 2022 at 12:11 PM.

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

    Re: VB.net Append text in the end of txt file

    Your title says:
    Append text in the end of txt file
    but your post says:
    add new value string in text file at specific location
    It would be helpful if your post didn't contradict your thread title.
    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

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

    Re: VB.net Append text in the end of txt file

    You can't really insert text into a file at an arbitrary point. You need to read the file, process the data as appropriate, then write all the data back to the file. You can call File.ReadAllLines to read the file into a String array. You can then modify any element of the returned array however you want; each one is just a String. You can then call File.WriteAllLines to write the array back to the file.

    If you want to add new lines at the same time, you can create a new List(Of String) and populate that from the array. You can then add, edit and remove lines as desired before passing the List to WriteAllLines.

    Before you ask, no I won't provide an "example", which really means write your code for you. I'm prepared to help further but I would expect to see an attempt to implement the advice already provided first.
    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

Tags for this Thread

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