Readini.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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() + @"\INSUConfigure.ini";
  25. if (System.IO.File.Exists(path) == false)
  26. {
  27. path = "C://ProgramData//XYS//XYSService//plugins//ReadYBCard//INSUConfigure.ini";
  28. }
  29. }
  30. catch (System.Exception ex)
  31. {
  32. path = "C://ProgramData//XYS//XYSService//plugins//ReadYBCard//INSUConfigure.ini";
  33. }
  34. }
  35. //类的构造函数,传递INI文件名
  36. //写INI文件
  37. public void IniWriteValue(string Section, string Key, string Value)
  38. {
  39. WritePrivateProfileString(Section, Key, Value, this.path);
  40. }
  41. //读取INI文件指定
  42. public string IniReadValue(string Section, string Key)
  43. {
  44. StringBuilder temp = new StringBuilder(255);
  45. int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
  46. return temp.ToString();
  47. }
  48. public string IniReadLogPath()
  49. {
  50. return IniReadValue("LOG", "path");
  51. }
  52. /// <summary>
  53. /// 获取本机的MAC地址
  54. /// </summary>
  55. /// <returns></returns>
  56. public string GetMACString()
  57. {
  58. ManagementClass mAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration");
  59. ManagementObjectCollection mo = mAdapter.GetInstances();
  60. foreach (ManagementBaseObject m in mo)
  61. {
  62. if ((bool)m["IpEnabled"] == true)
  63. {
  64. return m["MacAddress"].ToString();
  65. }
  66. }
  67. mo.Dispose();
  68. return null;
  69. }
  70. }
  71. }