// 声明INI文件的写操作函数 WritePrivateProfileString() [System.Runtime.InteropServices.DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); // 声明INI文件的读操作函数 GetPrivateProfileString() [System.Runtime.InteropServices.DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath); public static string WriteValue(string section, string key, string value) { string sPath = getIniFilePath(); long i = WritePrivateProfileString(section, key, value, sPath); if (i == 0) return "fail"; else return "success"; } public static string ReadValue(string section, string key) { string sPath = getIniFilePath(); StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(section, key, "", temp, 255, sPath); if (i == 0) return "fail"; else return temp.ToString(); } private static string getIniFilePath() { string sPath = null; if (System.Environment.CurrentDirectory == AppDomain.CurrentDomain.BaseDirectory)//windows应用程序则相等 sPath = AppDomain.CurrentDomain.BaseDirectory; else sPath = AppDomain.CurrentDomain.BaseDirectory + @"config.ini"; return sPath; }