C#读INI文件、写INI文件、删除INI节点、删除INI键
//API函数声明 [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); /// <summary> /// 读INI文件 /// </summary> /// <param name="Section">节点</param> /// <param name="Key">键名称</param> /// <param name="NoText"></param> /// <param name="iniFilePath">ini文件路径</param> /// <returns></returns> public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath) { if (File.Exists(iniFilePath)) { StringBuilder temp = new StringBuilder(10240); GetPrivateProfileString(Section, Key, NoText, temp, 10240, iniFilePath); return temp.ToString(); } else { return String.Empty; } } /// <summary> /// 写INI文件 /// </summary> /// <param name="Section">节点</param> /// <param name="Key">键名称</param> /// <param name="Value">键值</param> /// <param name="iniFilePath">ini文件路径</param> /// <returns></returns> public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath) { if (File.Exists(iniFilePath)) { long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath); if (OpStation == 0) { return false; } else { return true; } } else { return false; } } /// <summary> /// 删除节点 /// </summary> /// <param name="section">节点</param> /// <param name="filePath">ini文件路径</param> /// <returns></returns> public static long DeleteSection(string section, string filePath) { return WritePrivateProfileString(section, null, null, filePath); } /// <summary> /// 删除键 /// </summary> /// <param name="section">节点</param> /// <param name="key">键名称</param> /// <param name="filePath">ini文件路径</param> /// <returns></returns> public static long DeleteKey(string section, string key, string filePath) { return WritePrivateProfileString(section, key, null, filePath); }
评论前必须登录!
注册