Readini.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System.Text;
  2. using System.Runtime.InteropServices;
  3. using System.Management;
  4. using System.IO;
  5. using System.Reflection;
  6. namespace OperationLog
  7. {
  8. public class IniFile
  9. {
  10. public string path; //INI文件名
  11. public string HISIP;
  12. public string HISURL;
  13. public string HISauthorization;
  14. [DllImport("kernel32")]
  15. private static extern long WritePrivateProfileString(string section, string key,
  16. string val, string filePath);
  17. [DllImport("kernel32")]
  18. private static extern int GetPrivateProfileString(string section, string key, string def,
  19. StringBuilder retVal, int size, string filePath);
  20. //声明读写INI文件的API函数
  21. public IniFile()
  22. {
  23. //string spcfolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  24. //spcfolder = spcfolder.Replace("\\", "/");
  25. string curAssemblyFolder = new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath;
  26. path = Path.Combine(curAssemblyFolder, "INSUConfigure.ini"); ;
  27. }
  28. //类的构造函数,传递INI文件名
  29. //写INI文件
  30. public void IniWriteValue(string Section, string Key, string Value)
  31. {
  32. WritePrivateProfileString(Section, Key, Value, this.path);
  33. }
  34. //读取INI文件指定
  35. public string IniReadValue(string Section, string Key)
  36. {
  37. StringBuilder temp = new StringBuilder(255);
  38. int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
  39. return temp.ToString();
  40. }
  41. /// <summary>
  42. /// 获取本机的MAC地址
  43. /// </summary>
  44. /// <returns></returns>
  45. public string GetMACString()
  46. {
  47. ManagementClass mAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration");
  48. ManagementObjectCollection mo = mAdapter.GetInstances();
  49. foreach (ManagementBaseObject m in mo)
  50. {
  51. if ((bool)m["IpEnabled"] == true)
  52. {
  53. return m["MacAddress"].ToString();
  54. }
  55. }
  56. mo.Dispose();
  57. return null;
  58. }
  59. }
  60. }