close

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);
            }

 

arrow
arrow
    文章標籤
    C# INI
    全站熱搜

    抓狂小白 發表在 痞客邦 留言(0) 人氣()