首先在最外層給他一個宣告
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern long WritePrivateProfileString(string section,
string key, string val, string filePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern int GetPrivateProfileString(string section,
string key, string def, StringBuilder retVal,
int size, string filePath);
string iniPath = "Setup.ini";
string StartPath = Application.ExecutablePath.ToString();
在Form_Load指定ini檔名
iniPath = SetPath(iniPath);
iniGetSet();
接下來就是讀取跟寫入
中文的地方都是照自己要的填入
//ini
private string SetPath(string iniLogPath)
{
string path = "";
string[] arrPath = StartPath.Split('\\');
for (int i = 0; i < arrPath.Length - 1; i++)
{
path += arrPath[i];
path += "\\";
}
path += iniLogPath;
return path;
}
void iniGetSet()
{
int size = 3000;//temp file source size checkDay txtDay
StringBuilder temp = new StringBuilder(size); //temp file source
GetPrivateProfileString("類別名", "要讀的變數", "", temp, size, iniPath);
要讀的變數 = temp.ToString();
}
public static void iniWriteSet()
{
int size = 3000;//temp file source size checkDay txtDay
StringBuilder temp = new StringBuilder(size); //temp file source
WritePrivateProfileString("類別名", "寫入的變數", 寫入的變數值, iniPath);
}
ini檔裡就會長這樣
[類別名]
變數1=變數值1
變數2=變數值2
變數3=變數值3