using System.Text; using System.Runtime.InteropServices; using System.Management; using System.IO; using System.Reflection; namespace OperationLog { public class IniFile { public string path; //INI文件名 public string HISIP; public string HISURL; public string HISauthorization; [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文件的API函数 public IniFile() { //string spcfolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); //spcfolder = spcfolder.Replace("\\", "/"); string curAssemblyFolder = new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath; path = Path.Combine(curAssemblyFolder, "INSUConfigure.ini"); ; } //类的构造函数,传递INI文件名 //写INI文件 public void IniWriteValue(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, this.path); } //读取INI文件指定 public string IniReadValue(string Section, string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path); return temp.ToString(); } /// /// 获取本机的MAC地址 /// /// public string GetMACString() { ManagementClass mAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection mo = mAdapter.GetInstances(); foreach (ManagementBaseObject m in mo) { if ((bool)m["IpEnabled"] == true) { return m["MacAddress"].ToString(); } } mo.Dispose(); return null; } } }