4
Answers

Writing an ini file load routine in C#

Glenn Patton

Glenn Patton

9y
2.3k
1
Hi All,

Now that the pressure of a project has lifted (for the time being!) I would like opions and views on ini files what is the "correct" way to lay them out (format them and so on) where to keep them and the like I have done both of the below and would like to know if either of them are correct,
a) read the text file to a rich text box and do the following 

LocationStart = richTextBox1.Text.IndexOf(
// MessageBox.Show(LocationStart.ToString());//richTextBox1.Text.IndexOf()
MessageBox.Show(ValueA);//works
LocationStart+=2;
ValueA = richTextBox1.Text.Substring(LocationStart, 4);
"=");


this method works if you can garuntee the ini file will be in the location and has not been altered for the exact value

b)





{




Defaults = sr.ReadToEnd();
sr.Close();


}
String INIPath = Directory.GetCurrentDirectory();if (File.Exists(INIPath + "\\Test.ini"))StreamReader sr = new StreamReader(INIPath + "\\Test.ini");MessageBox.Show(INIPath);MessageBox.Show(Defaults);
 

and the below





{
a++;






Key[0] = Key[0].Trim();
Key[1] = Key[1].Trim();


COMA = Key[1];
string[] Values = Defaults.Split(delimetersChars);
foreach (string v in Values)char[] separator = { '=' };

string[] Key = v.Split(separator);
if (Key.Length != 2) continue;
if (Key[0] == "blah")

obviously blah should have a better name. this does all the file to edited and chopped around further.  The first method relies on the data being only so long and in a certain place.

Thanks
  Glenn


Answers (4)