先新增一個Class 來處理Ini 檔
public class IniFile
{
public string path;
[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 INIPath)
{
path = INIPath;
}
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp,255, this.path);
return temp.ToString();
}
}
黃色背景那段,主要是將kernel32.dll 匯入,並且使用dll裡的函式。當您要使用 IniFile 時, 要先去 new IniFile ,給予實體已進行執行。如下
IniFile ini = new IniFile(Application.StartupPath + "\\config\\config.ini");
並且讀取ini檔中section 與 value
string str = ini.IniReadValue("test", "test");
留言列表