我的编程世界

主办单位:个人站 联系方式: 1467512459@qq.com
备案号: 皖ICP备2023002383号-1

皖公网安备 34050402000593号

C#读写ini文件

类型:C#

"

调用
IniFile ini = new IniFile("config.ini");
string value = ini.ReadValue("Section", "Key");
ini.WriteValue("Section", "Key", "Value");


类库



using System.Runtime.InteropServices;
using System.Text;

class IniFile
{
    private string filePath;

    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

    public IniFile(string filePath)
    {
        this.filePath = filePath;
    }

    public void WriteValue(string section, string key, string value)
    {
        WritePrivateProfileString(section, key, value, filePath);
    }

    public string ReadValue(string section, string key)
    {
        StringBuilder sb = new StringBuilder(255);
        int i = GetPrivateProfileString(section, key, "", sb, 255, filePath);
        return sb.ToString();
    }
}
"

更新时间:2023-07-14 11:39:25