String - Find and replace

aliasmortal

Member
Joined
Feb 26, 2008
Messages
6
Programming Experience
1-3
Newbie help needed.

I am loading a text file into a string searching for a specific word and then replacing it, this works fine. My problem is, I need to search and find the word I am looking for but get the entire length of it, remove it, and replace it with the new text.
Below is my example, any help would be great.

[myinfo]
name="qwerty"
location="123 somewhere ave" <----e.g. This could be any length
occupation="whatever"

[nextinfo]

CODE SAMPLE:

*** newText gets passed to this function ***


Private Sub sysprepModification(ByVal newText As String)
Const ForReading = 1
Const ForWriting = 2


Dim objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile = objFSO.OpenTextFile("C:\info.inf", ForReading)
Dim strText = objFile.ReadAll

RichTextBox1.Text = strText
objFile.Close()

Dim strNewText = Replace(strText, "Name=*", "Name=" & newText)
objFile = objFSO.OpenTextFile("C:\info.inf", ForWriting)

objFile.WriteLine(strNewText)
objFile.Close()
RichTextBox2.Text = strNewText

End Sub
 
Since .inf files are ini-structured you should use a ini class to handle it. Here is one I wrote. Sample sets a new value for name key in myinfo section:
VB.NET:
Dim file As String = "test.inf"
Dim ini As New IniFile
ini.Load(file)
ini.Sections("myinfo").Items("name") = "new text"
ini.Save(file)
To search for all name keys in all sections you For-Each all sections, all keys.
 
Back
Top