多个网站域名 是新增接入,wordpress 公网访问,wordpress出现乱码,网站策划建设阶段的推广INI文件的读写操作在程序开发中#xff0c;很多人会把参数保存为ini文件格式#xff0c;ini文件的一个好处是可以保存为一个结构体主结构#xff0c;如 [User] Nametest UserIdtest [Server] ServerIp127.0.0.1 ServerPort80 …… 很方便也很容易区分#xff0c;而且不同节…INI文件的读写操作 在程序开发中很多人会把参数保存为ini文件格式ini文件的一个好处是可以保存为一个结构体主结构如 [User] Nametest UserIdtest [Server] ServerIp127.0.0.1 ServerPort80 …… 很方便也很容易区分而且不同节点下的名称可以重复。读取Name时只需要读取User节点下的Name。 ini文件的读写是需要调用windows的API来操作的。该API方法是在kernel32.dll中。 读ini文件调用 private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder refVal,int size, string filePath); 参数说明 sectionINI文件中的段落名称 keyINI文件中的关键字 def无法读取时候时候的缺省数值 retVal读取数值 size数值的大小 filePathINI文件的完整路径和名称 写ini文件调用 [DllImport(kernel32)] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); 参数说明 sectionINI文件中的段落 keyINI文件中的关键字 valINI文件中关键字的数值 filePathINI文件的完整的路径和名称 具体代码如下 public class IniFile{ private static string sIniFile Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), Config.ini); //参数说明sectionINI文件中的段落keyINI文件中的关键字valINI文件中关键字的数值filePathINI文件的完整的路径和名称 [DllImport(kernel32)] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); //参数说明sectionINI文件中的段落名称keyINI文件中的关键字def无法读取时候时候的缺省数值retVal读取数值 //size数值的大小filePathINI文件的完整路径和名称 [DllImport(kernel32)] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder refVal, int size, string filePath); public static string sUserId ””; public static string sName ””;public static string sServerIp ””;public static string sServerPort ””; #region 写入配置文件节/// summary/// 写入配置文件节/// /summarypublic static long WriteProfileString(string sSection, string sKey, string sVal, string sFileName){ return WritePrivateProfileString(sSection, sKey, sVal, sFileName);}#endregion #region 读取配置文件节/// summary/// 读取配置文件节/// /summarypublic static long GetProfileString(string sSection, string sKey, ref string refVal, string sFileName){ StringBuilder sVal new StringBuilder(); long lResult GetPrivateProfileString(sSection, sKey, refVal, sVal, 255, sFileName); refVal sVal.ToString(); return lResult;}#endregion #region 写入INI文件节/// summary/// 写入INI文件节/// /summarypublic static long WriteIniFileString(string sSection, string sKey, string sVal){ if (string.IsNullOrEmpty(sVal)) { return -1; } return WritePrivateProfileString(sSection, sKey, sVal, sIniFile);}#endregion #region 读取INI文件节/// summary/// 读取INI文件节/// /summarypublic static long GetIniFileString(string sSection, string sKey, ref string refVal){ StringBuilder sVal new StringBuilder(2048, 2048); long lResult GetPrivateProfileString(sSection, sKey, refVal, sVal, 1024, sIniFile); refVal sVal.ToString(); return lResult;}#endregion#region 读取配置文件/// summary/// 读取配置文件/// /summarypublic static bool ReadIniFile(){ try { // User GetIniFileString(User, UserId, ref sUserId); GetIniFileString(User, Name, ref sName); // Server GetIniFileString(Server, ServerIp, ref sServerIp); GetIniFileString(Server, ServerPort, ref sPort); return true; } catch (Exception ex) { return false; }}#endregion #region 写入配置文件/// summary/// 写入配置文件/// /summarypublic static bool WriteIniFile(){ try { // User WriteIniFileString(User, UserId, sUserId); WriteIniFileString(User, Name, sName); // Server WriteIniFileString(Server, ServerIp, sServerIp); WriteIniFileString(Server, ServerPort, sPort); return true; } catch (Exception ex) { return false; }}#endregion} 转载于:https://www.cnblogs.com/chhuic/archive/2010/08/27/1810358.html