Readini.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Text;
  2. using System.Runtime.InteropServices;
  3. namespace Ini
  4. {
  5. public class IniFile
  6. {
  7. public string path; //INI文件名 111
  8. public string HISIP;
  9. public string HISURL;
  10. public string HISauthorization;
  11. [DllImport("kernel32")]
  12. private static extern long WritePrivateProfileString(string section, string key,
  13. string val, string filePath);
  14. [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
  15. private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
  16. //声明读写INI文件的API函数
  17. public IniFile()
  18. {
  19. try
  20. {
  21. //path = System.IO.Directory.GetCurrentDirectory() + @"\PosConfigure.ini";
  22. path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\PosConfigure.ini";
  23. if (System.IO.File.Exists(path) == false)
  24. {
  25. path = "C://ProgramData//XYS//XYSService//plugins//ReadYBCard//PosConfigure.ini";
  26. }
  27. }
  28. catch (System.Exception)
  29. {
  30. path = "C://ProgramData//XYS//XYSService//plugins//ReadYBCard//PosConfigure.ini";
  31. }
  32. }
  33. //类的构造函数,传递INI文件名
  34. //写INI文件
  35. public void IniWriteValue(string Section, string Key, string Value)
  36. {
  37. WritePrivateProfileString(Section, Key, Value, this.path);
  38. }
  39. //读取INI文件指定
  40. public string IniReadValue(string Section, string Key)
  41. {
  42. StringBuilder temp = new StringBuilder(2048);
  43. int i = GetPrivateProfileString(Section, Key, "", temp, 2048, this.path);
  44. return temp.ToString();
  45. }
  46. public string IniReadLogPath()
  47. {
  48. return IniReadValue("LOG", "path");
  49. }
  50. }
  51. }