VS 2010 How to replace the words on a file ?

35 pts.
visual basic 2010
Windows 2012
So Im working on a program that shows this. 
This is the code that I use two show the SA and PLATINUM text because on the file they are not the only characters on the line, like this:
set username=sa
set password=platinum
code to search only those two words. 
 Dim lines() As String = IO.File.ReadAllLines("C:NTMBackup1NTMBackup4.bat")
        Dim userIndex As Integer = Array.FindIndex(lines, Function(s) s.Contains("utilizador"))
        Dim psswrdIndex As Integer = Array.FindIndex(lines, Function(s) s.Contains("password"))
        Me.txtusr.Text = lines(userIndex).Substring(lines(userIndex).IndexOf("="c)).Substring(1)
        Me.txtpassowrd.Text = lines(psswrdIndex).Substring(lines(psswrdIndex).IndexOf("="c)).Substring(1)
What I want is to change the "sa" and "platinum" textboxes and then save it on the file using the buttom, to change it on the flie like:
set username=something
set password=something
I made this code but it inserts on the whole line and it delete everything that is on the file, I think Im not using the correct code.
      Dim FILE_NAME As String = "C:NTMBackup1NTMBackup4.txt"
        If System.IO.File.Exists(FILE_NAME) = True Then
            Dim objwriter As New System.IO.StreamWriter(FILE_NAME)
        End If
If anyone got an idea please say anything, thanks :)

Software/Hardware used:
Visual Basic 2010 Windows 2012

Answer Wiki

Thanks. We'll let you know when a new response is added.

To avoid deleting everything that is in the file, you need to use the StreamWriter’s constructor that specifies whether you want to append text to your file, or replace it.

Do it this way:

Dim objwriter As New System.IO.StreamWriter(FILE_NAME<b>, true</b>)

More info:
<a href=”http://msdn.microsoft.com/en-us/library/36b035cb.aspx”>StreamWriter Constructor (String, Boolean)</a>

But since you don’t want to append those lines to the file, but to replace them, you might want to open the file the way you was doing it, but before writing the user and password, you would need to write all the previous lines that the file contains before the current user and password. You are already opening the file for reading (to locate the uses and password), so it should not be too difficult.

Discuss This Question: 1  Reply

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: