Editing Ini Files In Visual C#

947 views Asked by At

I am trying to use this to edit an ini, but I cannot seem to get the code to add the section of the ini file into the RichTextbox.

        if (myIni.GetSection(String.Format("[{0}]", comboBox1.SelectedItem.ToString().ToUpper())) != null);
        {
            mySection = new IniFile.IniSection(myIni, String.Format("{0}", comboBox1.SelectedItem.ToString().ToUpper()));
            foreach (IniFile.IniSection.IniKey key in mySection.Keys)
            {
                richTextBox1.AppendText(String.Format("{0}={1}{2}", key.Name, key.Value, Environment.NewLine));
            }
        }

I don't know what is wrong with my code.

1

There are 1 answers

0
SteveFerg On

You can use the old existing ini calls from windows:

[System.Runtime.InteropServices.DllImport("kernel32")]
static extern int GetPrivateProfileString(string section,
        string key, string def, StringBuilder retVal,
        int size, string filePath);
[System.Runtime.InteropServices.DllImport("kernel32")]
static extern long WritePrivateProfileString(string section,
        string key, string val, string filePath);

...

StringBuilder temp = new StringBuilder(255);
GetPrivateProfileString("section", "key", "default", temp, 255, inifilename);
String value = temp.ToString();


WritePrivateProfileString("section", "key", value, inifilename);