Utils.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Management;
  6. namespace SysSecLibs
  7. {
  8. public class Utils
  9. {
  10. public static Array StrToArray(string str)
  11. {
  12. return StrToArrayEx(str, ",");
  13. }
  14. /// <summary>
  15. /// 将一个字符串以分割符转换成Array对象,如 a,[b,[c,d]],e 将分割成 a ; b,[c,d] ; e 三个串
  16. /// </summary>
  17. /// <param name="str"></param>
  18. /// <param name="separator"></param>
  19. /// <returns></returns>
  20. public static Array StrToArrayEx(string str, string separator)
  21. {
  22. ArrayList list1 = new ArrayList();
  23. str = (str == null) ? "" : str.Trim();
  24. int num1 = separator.Length;
  25. while (str.Length > 0)
  26. {
  27. int num2 = str.IndexOf(separator);
  28. int num3 = str.IndexOf("[");
  29. if ((num3 >= 0) && (num3 < num2))
  30. {
  31. int num4 = 1;
  32. int num5 = str.Length;
  33. int num6 = num3 + 1;
  34. while (num6 < num5)
  35. {
  36. switch (str[num6])
  37. {
  38. case '[':
  39. num4++;
  40. goto Label_0082;
  41. case '\\':
  42. goto Label_0082;
  43. case ']':
  44. break;
  45. default:
  46. goto Label_0082;
  47. }
  48. num4--;
  49. Label_0082:
  50. if (num4 == 0)
  51. {
  52. break;
  53. }
  54. num6++;
  55. }
  56. num2 = str.IndexOf(separator, num6);
  57. }
  58. if (num2 < 0)
  59. {
  60. num2 = str.Length;
  61. }
  62. string text1 = str.Substring(0, num2);
  63. int num7 = text1.Length;
  64. if (((num7 > 0) && (text1[0] == '[')) && (text1[num7 - 1] == ']'))
  65. {
  66. text1 = text1.Substring(1, num7 - 2);
  67. }
  68. list1.Add(text1.Trim());
  69. if ((num2 + num1) > str.Length)
  70. {
  71. str = "";
  72. }
  73. else
  74. {
  75. str = str.Substring(num2 + num1).Trim();
  76. }
  77. }
  78. Array array1 = Array.CreateInstance(typeof(string), list1.Count);
  79. list1.CopyTo(array1);
  80. return array1;
  81. }
  82. public static string GetMachineMac()
  83. {
  84. string lcRetVal = "";
  85. try
  86. {
  87. string text2 = "select AdapterType, DeviceID, ProductName, ServiceName, MACAddress from Win32_NetworkAdapter";
  88. WqlObjectQuery query1 = new WqlObjectQuery(text2);
  89. ManagementObjectCollection collection1 = new ManagementObjectSearcher(query1).Get();
  90. foreach (ManagementObject obj1 in collection1)
  91. {
  92. lcRetVal += ((lcRetVal == "") ? "" : ",") + obj1["MACAddress"];
  93. }
  94. return lcRetVal;
  95. }
  96. catch (Exception)
  97. {
  98. }
  99. return lcRetVal;
  100. }
  101. public static string GetCpuId()
  102. {
  103. string lcRetVal = "";
  104. try
  105. {
  106. string text2 = "select CurrentClockSpeed, Name, OtherFamilyDescription, ProcessorId, ProcessorType, SystemName, UniqueId from Win32_Processor";
  107. WqlObjectQuery query1 = new WqlObjectQuery(text2);
  108. ManagementObjectCollection collection1 = new ManagementObjectSearcher(query1).Get();
  109. foreach (ManagementObject obj1 in collection1)
  110. {
  111. lcRetVal = lcRetVal + ((lcRetVal == "") ? "" : ",") + obj1["ProcessorId"];
  112. }
  113. return lcRetVal;
  114. }
  115. catch (Exception)
  116. {
  117. }
  118. return lcRetVal;
  119. }
  120. }
  121. }