using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace SWS.Commons { public static class IniHelper { [DllImport("kernel32", CharSet = CharSet.Unicode)] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32", CharSet = CharSet.Unicode)] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); /// ///读取ini文件数据 /// /// 段 /// 键名 /// 默认值 /// 文件路径 /// 读出内容 public static string ReadValueFromIniFile(string Section, string Key, string def = null, string filePath = null) { if (def == null) { def = ""; } if (filePath == null) { filePath = GlobalObject.configPath; } StringBuilder temp = new StringBuilder(4096); int i = GetPrivateProfileString(Section, Key, def, temp, 4096, filePath); return temp.ToString(); } /// /// 写入数据到ini文件 /// /// 段 /// 键名 /// 键值 /// 文件路径 public static void WriteValueFromIniFile(string Section, string Key, string Value, string filePath = null) { if (filePath == null) { filePath = GlobalObject.configPath; } WritePrivateProfileString(Section, Key, Value, filePath); } } }