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
Loading
VulpesPosted Feb 6, 2012, 12:09 PM
The former tend to be more robust and the .NET framework provides stacks of tools for reading and writing them. However, AFAIK there is no support for INI files other than the support for reading/writing text files generally.
Moses SpencerPosted Feb 10, 2015, 6:10 AM
http://www.dotnetperls.com/settings
But in some cases people do intend to stick with INI format, I'm not sure why... maybe because it is more readable then XML.
Nevertheless one thing you should note is that a provided .NET's configuration XML files are cached in run-time. So if you have updated it while your application was running, you need to call a refresh to retrieve those updated values (this is only for app.config, for web.config the web application is automatically restarted when something is changed in the file.
So that is why I would say that .config files are mostly suitable for read-only settings, while if you have some additional requirements or you need more freedom with choosing where to store file, how to handle validation and errors, more flexibility with its schema, etc. then I would suggest defining your custom settings file (even though this would require more development time.
Also just in case you are interested you can use this library to read INI formatted files with C#, for example like this:
Regards
Sam HobbsPosted Feb 15, 2012, 11:38 AM
Glenn PattonPosted Feb 6, 2012, 12:15 PM