CRegistration.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Management;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Xml;
  9. namespace OTSMeasureApp
  10. {
  11. class CRegistration
  12. {
  13. public bool RegistrationVerification()
  14. {
  15. if (!checkRegistration())//文件授权法
  16. {
  17. //return false;
  18. try
  19. {
  20. uint[] code;
  21. code = new uint[1];
  22. code[0] = 101;
  23. code[1] = 102;
  24. //狗加密法
  25. SenseShield.DogDecrypting.decrypting(code);//参数为许可号
  26. }
  27. catch (Exception ex)
  28. {
  29. return false;
  30. }
  31. }
  32. return true;
  33. }
  34. private bool checkRegistration()
  35. {
  36. string CPUID, DiskID;
  37. CPUID = getCpu();
  38. DiskID = GetDiskVolumeSerialNumber();
  39. var ID = ReadXML("./Config/SysData/RegistrationProofreading.txt");
  40. if (ID.Count == 0)
  41. {
  42. return false;
  43. }
  44. List<string> list_str = new List<string>();
  45. List<string> list_time = new List<string>();
  46. ID.TryGetValue("ID", out list_str);
  47. ID.TryGetValue("Time", out list_time);
  48. string setCPU = ConvertString(list_str[0]);
  49. string setDisk = ConvertString(list_str[1]);
  50. string setYear = ConvertString(list_time[0]);
  51. string setMonth = ConvertString(list_time[1]);
  52. string setDay = ConvertString(list_time[2]);
  53. if (CPUID != setCPU || DiskID != setDisk)
  54. {
  55. return false;
  56. }
  57. DateTime dt = DateTime.ParseExact(setYear + setMonth + setDay + "235959", "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  58. if (DateTime.Now > dt)
  59. {
  60. return false;
  61. }
  62. return true;
  63. }
  64. // 获得CPU的序列号
  65. private static string getCpu()
  66. {
  67. string strCpu = null;
  68. ManagementClass myCpu = new ManagementClass("win32_Processor");
  69. ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
  70. foreach (ManagementObject myObject in myCpuConnection)
  71. {
  72. strCpu = myObject.Properties["Processorid"].Value.ToString();
  73. break;
  74. }
  75. return strCpu;
  76. }
  77. // 取得设备硬盘的卷标号
  78. private static string GetDiskVolumeSerialNumber()
  79. {
  80. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  81. ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
  82. disk.Get();
  83. return disk.GetPropertyValue("VolumeSerialNumber").ToString();
  84. }
  85. private static IDictionary<String, List<String>> ReadXML(string address)
  86. {
  87. IDictionary<String, List<String>> infos = new Dictionary<String, List<String>>();
  88. if (File.Exists(address))
  89. {
  90. XmlDocument xmlDoc = new XmlDocument();
  91. xmlDoc.Load(address);
  92. XmlNode xn = xmlDoc.SelectSingleNode("infos");
  93. XmlNodeList xnl = xn.ChildNodes;
  94. foreach (XmlNode xnf in xnl)
  95. {
  96. XmlElement xe = (XmlElement)xnf;
  97. XmlNode nameNode = xe.SelectSingleNode("ID");
  98. string name = nameNode.InnerText;
  99. Console.WriteLine(name);
  100. XmlNode filelist = xe.SelectSingleNode("filelist");
  101. List<String> list = new List<string>();
  102. foreach (XmlNode item in filelist.ChildNodes)
  103. {
  104. list.Add(item.InnerText);
  105. }
  106. infos.Add(name, list);
  107. }
  108. }
  109. return infos;
  110. }
  111. private string ConvertString(string str)
  112. {
  113. string str_out;
  114. string[] arr = str.Split(',');
  115. byte[] be = new byte[arr.Count()];
  116. for (int i = 0; i < arr.Count(); i++)
  117. {
  118. be[i] = Convert.ToByte(arr[i]);
  119. }
  120. str_out = Encoding.Unicode.GetString(be);
  121. return str_out;
  122. }
  123. }
  124. }