Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Edit cfg file with user-specified data

Status
Not open for further replies.

evolution445

Technical User
Joined
Sep 2, 2007
Messages
4
Hi all,

In my program a user can specify a path where an .cfg file is located. I also got some textboxes where users can add their own data. When they click the Start button I want my program to search and change some strings inside the .cfg file.

I already attempted my problem with this:
Code:
Dim Readtxt As System.IO.StreamReader
        Dim Writetxt As System.IO.StreamWriter
        System.IO.File.OpenText(folder.Text & "\data.cfg"

But from there on Im stuck and don't know how to proceed.

Thanks in advance
 
Sorry for this repost but I think I haven't really explained my problem very well.

What I want to achieve is: A user fills Textbox1 with "Test", then once the user clicks the Start button, I want the program to search for a certain string ( String1 = "ABCDE" ) in a cfg file and change it ( String = "Test" )

I hope there is someone here who knows what I mean and knows how this is done.

Thanks in advance
 
Thanks for your reply Rick, but it doesn't seem to work in my situation, since it's not an XML file I'm reading/changing.

When my program is started the user will be prompted with a Browse folder dialog. Then they have to select the path where the Data.cfg file is located. This will then be saved to an invisible label.

Next up I got a few textboxes and a checkbox. Let's say the user writes "Test1" in textbox 1, and "Text2" in textbox 2. Then ticks the Off checkbox.

Then if they press Start, I want my program to use the earlier saved path. (label1.text & "\data.cfg") And then start searching for several strings.
( this = "12345" )
( that = "54321" )
( enabled = 1 )
And then change them to the settings the user entered.
( this = "Test1" )
( that = "Test2" )
( enabled = 0 )

I hope you understand what Im trying to achieve.

Thanks in advance
 
two things:

1) Invisible labels were a bad idea in VB6, and continue to be bad ideas in VB.Net. I would recommend storing the value in a variable at the form level instead.

2) I believe so, but is the .cfg file you're working with of your own creation? Or is it a .cfg for another application that you are trying to modify?

If it is your own cfg, you can replace it entirely with the code in that FAQ and never have to mess with the file itself. If it is a separate entity, it gets a little more challenging, but is still very do-able.

Code:
dim SearchStrings() as string = {"this","that", "enabled"}
dim NewValues() as string = {"Test1", "Test2", "0"}
dim Lines() as string = io.file.ReadAllLines(filename)
for each Line as string in Lines
  for i as integer = 0 to SearchStrings.length-1
    if line.contains(SearchStrings(i) & " = ") then
       line = Line.Substring(0, Line.IndexOf("""")) & newvalues(i) & """"
    end if
  next
next

or something like that.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Hi Rick,

The .cfg file I'm trying to modify is from another .exe application. I attempted it again a few times with the code you provided but it doesn't seem to work.
I was wondering maybe if I did something wrong with the SearchStrings. I only wrote the value it has to search for between the quotes. Do I also need to add the amount of spaces till the "=" sign or will it read over that ?

The formatting in the .cfg file looks a bit like this:

windowed = 0
country = "EU"
red_password = ""
blue_password = ""

Then I enter ("windowed", "country", "red_password", "blue_password") in SearchStrings,
and ("1", "GB", "redteam", "blueteam") in NewValues.

Is this the correct way ?

Thanks in Advance
 
Is the space between the last letter of the identifier and the = consistent? or is the = a set number of characters in? You will have to find a way to pad the searchstring(i) so that the = lines up with what is expect. Optionally, you could just drop the = sign, but then there is the possibility that you could have false positives.

If the identifier is always at the same place on every line I would get rid of the .contains all together and just check the value at that location using .substring.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top