using System.Text; using System.Runtime.InteropServices; namespace Ini { public class IniFile { public string path; //INI文件名 111 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.dll", CharSet = CharSet.Unicode)] private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); //声明读写INI文件的API函数 public IniFile() { try { //path = System.IO.Directory.GetCurrentDirectory() + @"\PosConfigure.ini"; path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\PosConfigure.ini"; if (System.IO.File.Exists(path) == false) { path = "C://ProgramData//XYS//XYSService//plugins//ReadYBCard//PosConfigure.ini"; } } catch (System.Exception) { path = "C://ProgramData//XYS//XYSService//plugins//ReadYBCard//PosConfigure.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(2048); int i = GetPrivateProfileString(Section, Key, "", temp, 2048, this.path); return temp.ToString(); } public string IniReadLogPath() { return IniReadValue("LOG", "path"); } } }