using System.Runtime.InteropServices; //使用Winapi 讀寫 INI
...
//INI副程式
#region SetupIni
public class SetupIni
{
public string path;
[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);
public void IniWriteValue(string Section, string Key, string Value, string inipath)
{
WritePrivateProfileString(Section, Key, Value, Application.StartupPath + "\\" + inipath);
}
public string IniReadValue(string Section, string Key, string inipath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, Application.StartupPath + "\\" + inipath);
return temp.ToString();
}
}
#endregion
//INI宣告
public string filename = "setup.ini";
SetupIni ini = new SetupIni();
//INI 讀取
if (File.Exists(Application.StartupPath + "\\" + filename))
{
foreach (Control c in tp_Setup.Controls)
{
if (c is TextBox)
c.Text = ini.IniReadValue("setup", c.Name, filename);
}
}
//INI寫入
foreach (Control c in tp_Setup.Controls)
{
if (c is TextBox)
ini.IniWriteValue("setup", c.Name, c.Text, filename);
}
留言列表