Readini.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Text;
  2. using System.Runtime.InteropServices;
  3. using System.Management;
  4. using System;
  5. namespace Ini
  6. {
  7. public class IniFile
  8. {
  9. public string path; //INI文件名 111
  10. public string HISIP;
  11. public string HISURL;
  12. public string HISauthorization;
  13. [DllImport("kernel32")]
  14. private static extern long WritePrivateProfileString(string section, string key,
  15. string val, string filePath);
  16. [DllImport("kernel32")]
  17. private static extern int GetPrivateProfileString(string section, string key, string def,
  18. StringBuilder retVal, int size, string filePath);
  19. //声明读写INI文件的API函数
  20. public IniFile()
  21. {
  22. try
  23. {
  24. //path = System.IO.Directory.GetCurrentDirectory() + @"\PosConfigure.ini";
  25. path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\PosConfigure.ini";
  26. if (System.IO.File.Exists(path) == false)
  27. {
  28. path = "C://ProgramData//XYS//XYSService//plugins//ReadYBCard//PosConfigure.ini";
  29. }
  30. }
  31. catch (System.Exception)
  32. {
  33. path = "C://ProgramData//XYS//XYSService//plugins//ReadYBCard//PosConfigure.ini";
  34. }
  35. }
  36. //类的构造函数,传递INI文件名
  37. //写INI文件
  38. public void IniWriteValue(string Section, string Key, string Value)
  39. {
  40. WritePrivateProfileString(Section, Key, Value, this.path);
  41. }
  42. //读取INI文件指定
  43. public string IniReadValue(string Section, string Key)
  44. {
  45. StringBuilder temp = new StringBuilder(255);
  46. int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
  47. return temp.ToString();
  48. }
  49. public string IniReadLogPath()
  50. {
  51. return IniReadValue("LOG", "path");
  52. }
  53. /// <summary>
  54. /// 获取本机的MAC地址
  55. /// </summary>
  56. /// <returns></returns>
  57. public string GetMACString()
  58. {
  59. ManagementClass mAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration");
  60. ManagementObjectCollection mo = mAdapter.GetInstances();
  61. foreach (ManagementBaseObject m in mo)
  62. {
  63. if ((bool)m["IpEnabled"] == true)
  64. {
  65. return m["MacAddress"].ToString();
  66. }
  67. }
  68. mo.Dispose();
  69. return null;
  70. }
  71. }
  72. }