Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite a specific line in a text file using VB.NET

Tags:

parsing

vb.net

I need to do the following:

Change the line in a text file

[Path] = "c:\this\certain\path\"

with this line

[Path] = "c:\that\other\newer\path\"

These paths will most certainly be different lengths, so I need to either replace what's in the quotes or erase the line completely and enter a new one, but in the same spot, not appended to the end of the document.

like image 847
Niphoet Avatar asked Jan 12 '10 15:01

Niphoet


People also ask

How do I append to a text file in Visual Basic?

To append to a text fileUse the WriteAllText method, specifying the target file and string to be appended and setting the append parameter to True . This example writes the string "This is a test string." to the file named Testfile.


1 Answers

This will do the trick

    Dim thefile As String = "filepath"
    Dim lines() As String = System.IO.File.ReadAllLines("filepath")

    lines(number of line you want to replace) = "write what you want to replace here"

    System.IO.File.WriteAllLines(filepath, lines)
like image 56
Raymond C Borges Hink Avatar answered Oct 01 '22 04:10

Raymond C Borges Hink