Form1.cs 6.6 KB

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