基於C#winform設計。
首先創建一個類,我命名為IniFiles。並引入命名空間using System.Runtime.InteropServices;
接着,聲明API函數
[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);
寫入INI函數方法

/// <summary> /// 寫入INI文件 /// </summary> /// <param name="Section">項目名稱(如 [TypeName] )</param> /// <param name="Key">鍵</param> /// <param name="Value">值</param> public void IniWriteValue(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, this.inipath); }
讀取INI文件方法

/// <summary> /// 讀出INI文件 /// </summary> /// <param name="Section">項目名稱(如 [TypeName] )</param> /// <param name="Key">鍵</param> public string IniReadValue(string Section, string Key) { StringBuilder temp = new StringBuilder(500); int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath); return temp.ToString(); }
驗證文件是否存在

/// <summary> /// 驗證文件是否存在 /// </summary> /// <returns>布爾值</returns> public bool ExistINIFile() { return File.Exists(inipath); }
在其他窗體頁面如何條用?請看:
這時候是創建INI文件(位置一般處於資源文件下bin\Debug目錄)
public partial class Frm_Login : Form { HotelSystemORM.Unitl.IniFiles ini = new HotelSystemORM.Unitl.IniFiles(Application.StartupPath + @"\MyConfig.INI");//Application.StartupPath只適用於winform窗體程序 }
生成了文件之后就可以寫入和讀取信息了。
ini.IniWriteValue("登入詳細", "賬號", "test"); ini.IniWriteValue("登入詳細", "密碼", "password");
讀取登入信息(頁面加載的時候)
if (ini.ExistINIFile())//驗證是否存在文件,存在就讀取 label1.Text = ini.IniReadValue("登入詳細", "用戶名");
完成!
IniFiles類全部完整代碼
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Runtime.InteropServices; namespace WindowsFormsApplication1 { public class IniFiles { public string inipath; //聲明API函數 [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); /// <summary> /// 構造方法 /// </summary> /// <param name="INIPath">文件路徑</param> public IniFiles(string INIPath) { inipath = INIPath; } public IniFiles() { } /// <summary> /// 寫入INI文件 /// </summary> /// <param name="Section">項目名稱(如 [TypeName] )</param> /// <param name="Key">鍵</param> /// <param name="Value">值</param> public void IniWriteValue(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, this.inipath); } /// <summary> /// 讀出INI文件 /// </summary> /// <param name="Section">項目名稱(如 [TypeName] )</param> /// <param name="Key">鍵</param> public string IniReadValue(string Section, string Key) { StringBuilder temp = new StringBuilder(500); int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath); return temp.ToString(); } /// <summary> /// 驗證文件是否存在 /// </summary> /// <returns>布爾值</returns> public bool ExistINIFile() { return File.Exists(inipath); } } }
頁面調用: