//用法: string s; int i; IniFile ini;//聲明 ini = new IniFile(@"C:\Users\601\Desktop\1.ini");//實例 ini.setInt("secname1", "keyname1", i);寫入 s = ini.getInt("secname1", "keyname1", 0).ToString(); //讀出0 is default value //IniFile類的定義 using System.Runtime.InteropServices; class IniFile { private string pathname; #region KERNEL32.DLL [DllImport("Kernel32.dll")] public static extern UInt32 GetPrivateProfileInt( string lpAppName, string lpKeyName, int nDefault, string lpFileName ); [DllImport("Kernel32.dll")] public static extern UInt32 GetPrivateProfileString( string lpAppName, string lpKeyName, string lpDefault, out string lpReturnedString, UInt32 nSize, string lpFileName ); [DllImport("Kernel32.dll")] public static extern bool WritePrivateProfileString( string lpAppName, string lpKeyName, string lpString, string lpFileName ); #endregion public IniFile(string pathname) { this.pathname = pathname; } public bool setInt(string szSection, string szKey, int iVal) { return WritePrivateProfileString(szSection, szKey, iVal.ToString(), pathname); } public bool setString(string szSection, string szKey, string szVal) { return WritePrivateProfileString(szSection, szKey, szVal, pathname); } public UInt32 getInt(string szSection, string szKey, int iDef) { return GetPrivateProfileInt(szSection, szKey, iDef, pathname); } public string getString(string szSection, string szKey, string szDef) { string ret; GetPrivateProfileString(szSection, szKey, szDef, out ret, 255, pathname); return ret; } } |
|