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

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

VB
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.
Posted
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:
C#
IO.File.WriteAllText(FilePath, IO.File.ReadAllText(FilePath).Replace("Unnamed", TextBox_NewUser.Text)
 
Share this answer
 
Comments
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
VB
'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