Form1.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Management;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using System.Xml;
  13. namespace RegistrationAuthorization
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. private void button1_Click(object sender, EventArgs e)
  22. {
  23. SaveFileDialog sfd = new SaveFileDialog();
  24. sfd.Filter = "(*.txt)|*.txt|(*.*)|*.*";
  25. if (sfd.ShowDialog() == DialogResult.OK)
  26. {
  27. IDictionary<String, List<String>> infos = new Dictionary<String, List<String>>();
  28. infos.Add("ID", new List<string>() { getCpu(), GetDiskVolumeSerialNumber() });
  29. infos.Add("Time", new List<string>() { DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString(), DateTime.Now.Day.ToString() });
  30. SaveXML(infos, sfd.FileName);
  31. MessageBox.Show("Save complete");
  32. }
  33. }
  34. // 获得CPU的序列号
  35. static string getCpu()
  36. {
  37. string strCpu = null;
  38. ManagementClass myCpu = new ManagementClass("win32_Processor");
  39. var myCpuConnection = myCpu.GetInstances();
  40. foreach (ManagementObject myObject in myCpuConnection)
  41. {
  42. strCpu = myObject.Properties["Processorid"].Value.ToString();
  43. break;
  44. }
  45. return strCpu;
  46. }
  47. // 取得设备硬盘的卷标号
  48. static string GetDiskVolumeSerialNumber()
  49. {
  50. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  51. ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
  52. disk.Get();
  53. return disk.GetPropertyValue("VolumeSerialNumber").ToString();
  54. }
  55. public static void SaveXML(IDictionary<String, List<String>> infos, string address)
  56. {
  57. if (infos == null || infos.Count == 0)
  58. {
  59. return;
  60. }
  61. XmlDocument xmlDoc = new XmlDocument();
  62. XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
  63. xmlDoc.AppendChild(dec);
  64. XmlElement _infos = xmlDoc.CreateElement("infos");
  65. foreach (KeyValuePair<String, List<String>> item in infos)
  66. {
  67. XmlElement info = xmlDoc.CreateElement("info");
  68. XmlElement name = xmlDoc.CreateElement("ID");
  69. name.InnerText = item.Key;
  70. info.AppendChild(name);
  71. XmlNode filelist = xmlDoc.CreateElement("filelist");
  72. info.AppendChild(filelist);
  73. foreach (String number in item.Value)
  74. {
  75. XmlElement filed = xmlDoc.CreateElement("filed");
  76. filed.InnerText = number;
  77. filelist.AppendChild(filed);
  78. }
  79. _infos.AppendChild(info);
  80. }
  81. xmlDoc.AppendChild(_infos);
  82. xmlDoc.Save(address);
  83. }
  84. private void Form1_Load(object sender, EventArgs e)
  85. {
  86. string CPUID, DiskID;
  87. CPUID = getCpu();
  88. DiskID = GetDiskVolumeSerialNumber();
  89. var ID = ReadXML("./Config/SysData/RegistrationProofreading.txt");
  90. List<string> list_time = new List<string>();
  91. List<string> list_str = new List<string>();
  92. ID.TryGetValue("ID", out list_str);
  93. ID.TryGetValue("Time", out list_time);
  94. ID.TryGetValue("ID", out list_str);
  95. ID.TryGetValue("Time", out list_time);
  96. string setCPU = ConvertString(list_str[0]);
  97. string setDisk = ConvertString(list_str[1]);
  98. if (CPUID != setCPU || DiskID != setDisk)
  99. {
  100. label1.Text = "缺少授权!";
  101. label2.Visible = false;
  102. }
  103. else
  104. {
  105. label2.Visible = true;
  106. DateTime dt = DateTime.ParseExact(ConvertString(list_time[0]) + ConvertString(list_time[1]) + ConvertString(list_time[2]) + "235959", "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  107. if (DateTime.Now > dt)
  108. {
  109. label1.Text = "授权过期!";
  110. }
  111. else
  112. {
  113. label1.Text = "授权已激活";
  114. }
  115. label2.Text = "有效期至:" + ConvertString(list_time[0]) + "-" + ConvertString(list_time[1]) + "-" + ConvertString(list_time[2]);
  116. }
  117. }
  118. public static IDictionary<String, List<String>> ReadXML(string address)
  119. {
  120. IDictionary<String, List<String>> infos = new Dictionary<String, List<String>>();
  121. if (File.Exists(address))
  122. {
  123. XmlDocument xmlDoc = new XmlDocument();
  124. xmlDoc.Load(address);
  125. XmlNode xn = xmlDoc.SelectSingleNode("infos");
  126. XmlNodeList xnl = xn.ChildNodes;
  127. foreach (XmlNode xnf in xnl)
  128. {
  129. XmlElement xe = (XmlElement)xnf;
  130. XmlNode nameNode = xe.SelectSingleNode("ID");
  131. string name = nameNode.InnerText;
  132. Console.WriteLine(name);
  133. XmlNode filelist = xe.SelectSingleNode("filelist");
  134. List<String> list = new List<string>();
  135. foreach (XmlNode item in filelist.ChildNodes)
  136. {
  137. list.Add(item.InnerText);
  138. }
  139. infos.Add(name, list);
  140. }
  141. }
  142. return infos;
  143. }
  144. private string ConvertString(string str)
  145. {
  146. string str_out;
  147. string[] arr = str.Split(',');
  148. byte[] be = new byte[arr.Count()];
  149. for (int i = 0; i < arr.Count(); i++)
  150. {
  151. be[i] = Convert.ToByte(arr[i]);
  152. }
  153. str_out = Encoding.Unicode.GetString(be);
  154. return str_out;
  155. }
  156. }
  157. }