0

So for fun, I decided to write a tool that loads a game's ini file(Outlast) and then lets you change the values for certain things.

The way I did this was, in the original ini, I found the line number the variable was on, and then removed the variable name in the code leaving only the value it held, and it wrote that to the textbox.

I am horrible at explaining things, so to clarify, in this particular ini file this is what line 42 is:

NormalWalkSpeed=200,

And in the code, I did this to write that number to a textbox:

WalkSpeedTxtBox.Text = GameLocation.DefaultGameIniLines[42].Replace("NormalWalkSpeed=", "");

And to change it, this is the button's code:

GameLocation.DefaultGameIniLines[42] = "NormalWalkSpeed=" + walkspeed;
System.IO.File.WriteAllLines(GameLocation.DefaultGameIniLocation, GameLocation.DefaultGameIniLines);
MessageBox.Show("Success! Walk speed has been set to " + WalkSpeedTxtBox.Text);
System.Media.SystemSounds.Beep.Play();

So the problem right off the bat with this is, obviously, if the ini file is not exactly the same as it was before, as in the lines change, this will no longer work and I'd have to rewrite everything.

What exactly would be a better way of doing what I want to do?

Revan114
  • 117
  • 1
  • 4
  • 10
  • That has been asked and answered: http://stackoverflow.com/questions/217902/reading-writing-an-ini-file?rq=1 – Steve Wellens Feb 23 '15 at 05:25
  • You should look at what is before the `=` character - that's the name of the variable. Find the right one, then read/modify what's after the `=` - that's the value. – vesan Feb 23 '15 at 05:25

2 Answers2

0

INI files are structured configuration files, which allow you to find a value given its Group and Key. Treating it as a sequential collection of lines will miss that point - and treating each line as simply a bunch of text means more work for you.

Assuming you want to write this code yourself rather than using an existing INI reading library, the best thing to do is to run over the INI file, converting it into a Dictionary of Dictionaries. A Dictionary is the data structure that makes most sense, since it allows you to access a collection of Keys by their Group name, and a value by its Key name.

Something like this:

Dictionary<string, Dictionary<string,string>> Groups = new...
string currentGroupName;
foreach (string line in File.ReadAllLines(iniFile))
{
    if (lineStartsWith("[") && line.EndsWith("]"))
    {
        currentGroupName = line.Substring(1, line.Length-2);
        Groups.Add(currentGroupName, new Dictionary<string,string>());
    }
    else if (line.Contains("="))
    {
        string[] keyValue = line.Split("=");
        Groups[currentGroupName].Add(keyValue[0], keyValue[1]);
    }

}

This, roughly, will allow you to iterate on all the groups and their keys, getting the values. You can then construct a list of Strings back from it to save.

Community
  • 1
  • 1
Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
-1

you could try to find that string in file and if found you can fetch its value

  using (var reader = new StreamReader(file))
    {
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            if (line.Contains("NormalWalkSpeed="))
                {
                   //get its value
                }
         }
    }
goldsmit409
  • 478
  • 4
  • 26