using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace Api.Framework.Tools { /// /// ini配置文件操作类 /// public class IniHelper { [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filepath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath); //获取ini文件路径 private string inifilepath; /// /// ini文件完整路径 /// /// public IniHelper(string fileName) { this.inifilepath = fileName; } /// /// 获取ini配置文件中指定key的数据 /// /// 头 /// 头下面的key /// public string GetValue(string node, string key) { StringBuilder s = new StringBuilder(1024); GetPrivateProfileString(node, key, "", s, 1024, inifilepath); return s.ToString(); } /// /// 设置ini配置文件的字段 /// /// 头 /// 键 /// 值 public void SetValue(string node, string key, string value) { try { WritePrivateProfileString(node, key, value, inifilepath); } catch (Exception ex) { throw ex; } } } }