C# 讀寫 ini 檔案
使用 kernel32.DLL
引用:using System.Runtime.InteropServices; //DllImport
範例:
class clsINI
{
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(string section, string key, string def, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
private string iniFileName;
//private bool bDisposed = false;
public clsINI(string xfilepath)
{
iniFileName = xfilepath;
}
public void WriteIni(string section, string key, string val)
{
WritePrivateProfileString(section, key, val, iniFileName);
}
public string ReadIni(string section, string key)
{
StringBuilder temp = new StringBuilder(255);
GetPrivateProfileString(section, key, "", temp, 255, iniFileName);
return temp.ToString();
}
}
參考:
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-writeprivateprofilestringa
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprivateprofilestring
沒有留言:
張貼留言