在 Visual C# 中编辑 Ini 文件

本文关键字:Ini 文件 编辑 Visual | 更新日期: 2023-09-27 17:56:09

我正在尝试使用它来编辑 ini,但我似乎无法获取将 ini 文件部分添加到 RichText 框中的代码。

        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));
            }
        }

我不知道我的代码出了什么问题。

在 Visual C# 中编辑 Ini 文件

您可以从 Windows 使用旧的现有 ini 调用:

[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);