Click here to Skip to main content
15,746,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am new to and am trying to do something really simple. I have this code which reads certain line of text from .ini file.

Dim FilePath As String

FilePath = Application.StartupPath & "\bin\userconfig.ini"
IO.File.WriteAllText(FilePath, IO.File.ReadAllText(FilePath).Replace("UserName = ", TextBox_NewUser.Text)

How do I make it replace that line of text after the "=" with something you type in TextBox_NewUser. As you can see with current code it just replaces the whole "UserName =" which I don't want.

That specific line of text in the .ini by default has this value:
"UserName = Unnamed"

So how do I make it replace just that "Unnamed" with something I type in TextBox_NewUser?

Any assistance will be most appreciated.

What I have tried:

I haven't tried much since I'm a noob in VS.
Updated 2-May-23 2:01am

It should be obvious that it replaces the "UserName = " part because this is exactly what you are asking it to do.
Why not replacing the "Unnamed" part instead? Something like:
IO.File.WriteAllText(FilePath, IO.File.ReadAllText(FilePath).Replace("Unnamed", TextBox_NewUser.Text)
Share this answer
Darius47 21-Mar-16 9:23am    
Thank you for your reply, but you see that "Unnamed" is variable and could be anything. So if I'd try to change my name to something else yet again, it wouldn't work.
This is something really easy, see below snippet
'import System.IO
dim text as string = File.ReadAllText("test.txt")
text = text.Replace("some text", "new value")
File.WriteAllText("test.txt", text)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900