SystemInfoFrm.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.Security.Cryptography;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12. namespace MachineFrm
  13. {
  14. public partial class SystemInfoFrm : Form
  15. {
  16. static string valsystem ;
  17. static string valmac ;
  18. static string valcpu ;
  19. static string valboard;
  20. public SystemInfoFrm()
  21. {
  22. InitializeComponent();
  23. }
  24. private void btnGet_Click(object sender, EventArgs e)
  25. {
  26. btnGet.Enabled = false;
  27. Thread.Sleep(3000);
  28. GetMachineInfo();
  29. btnGet.Enabled = true;
  30. MessageBox.Show("已经重新获取了机器信息!");
  31. }
  32. private void btnExit_Click(object sender, EventArgs e)
  33. {
  34. Application.Exit();
  35. }
  36. private void SystemInfoFrm_Load(object sender, EventArgs e)
  37. {
  38. string a = GetKey();
  39. string b = GetKey2();
  40. GetMachineInfo();
  41. }
  42. public string GetKey()
  43. {
  44. string errorinfo = string.Empty;
  45. string val1 = "BFEBFBFF000906E9";
  46. string val2 = "/3B2LKQ2/CNFCW0086402KS/";
  47. return GetKeyInfo("",val1,val2,ref errorinfo);
  48. }
  49. public string GetKey2()
  50. {
  51. string errorinfo = string.Empty;
  52. string val1 = "BFEBFBFF000306C3";
  53. string val2 = "160162955501177";
  54. return GetKeyInfo("", val1, val2, ref errorinfo);
  55. }
  56. public void GetMachineInfo()
  57. {
  58. try
  59. {
  60. string a = Guid.NewGuid().ToString();
  61. valsystem = MachineInfo.GetSystemName() + "-" + MachineInfo.GetSystemType();
  62. valmac = MachineInfo.GetMacAddress();
  63. valcpu = MachineInfo.GetCpuID();
  64. valboard = MachineInfo.GetMotherBoardID();
  65. txtSystem.Text = valsystem;
  66. txtMac.Text = valmac;
  67. txtCpu.Text = valcpu;
  68. txtBoard.Text = valboard;
  69. }
  70. catch (Exception ex)
  71. {
  72. MessageBox.Show(ex.Message.ToString());
  73. }
  74. }
  75. private void btnExport_Click(object sender, EventArgs e)
  76. {
  77. try
  78. {
  79. FolderBrowserDialog ofd = new FolderBrowserDialog();
  80. if (ofd.ShowDialog() == DialogResult.OK)
  81. {
  82. string targetPath = ofd.SelectedPath;
  83. string filename = targetPath + Path.DirectorySeparatorChar + "SystemInfoFrm" + ".txt";
  84. if (!Directory.Exists(targetPath))//验证路径是否存在
  85. {
  86. Directory.CreateDirectory(targetPath);
  87. //不存在则创建
  88. }
  89. FileStream fs;
  90. StreamWriter sw;
  91. if (File.Exists(filename))
  92. //验证文件是否存在,有则追加,无则创建
  93. {
  94. fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
  95. }
  96. else
  97. {
  98. fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
  99. }
  100. sw = new StreamWriter(fs);
  101. //sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") );
  102. sw.WriteLine("[SystemInfoFrm]获取机器基本信息。time:" + DateTime.Now.ToString());
  103. //sw.WriteLine("------------------------------------------------------------------");
  104. sw.WriteLine($"操作系统:{valsystem}");
  105. sw.WriteLine($"mac地址:{valmac}");
  106. sw.WriteLine($"cpu序号:{valcpu}");
  107. sw.WriteLine($"主板序号:{valboard}");
  108. //sw.WriteLine("------------------------------------------------------------------");
  109. sw.Close();
  110. fs.Close();
  111. }
  112. MessageBox.Show("已成功导出SystemInfoFrm.txt文件!");
  113. }
  114. catch (Exception ex)
  115. {
  116. MessageBox.Show(ex.Message.ToString());
  117. }
  118. }
  119. private string GetKeyInfo(string keyone, string keytwo, string keythree, ref string errorinfo)
  120. {
  121. try
  122. {
  123. if (keyone == "unknown" || string.IsNullOrEmpty(keyone))
  124. {
  125. //errorinfo = "GetMacAddress[unknown] ";
  126. //return "";
  127. keyone = "";
  128. }
  129. if (keytwo == "unknown")
  130. {
  131. errorinfo = "GetCpuID[unknown] ";
  132. return "";
  133. }
  134. if (keythree == "unknown")
  135. {
  136. errorinfo = "GetMotherBoardID[unknown] ";
  137. return "";
  138. }
  139. string k1 = "9d6a61dc-d5cf-4199-81b0-d4bb3ee6f613";
  140. string k2 = "b41f7f33-28a8-48fa-876a-87892f95d7f9";
  141. string str = $"{keyone}{k1}{keytwo}{k2}{keythree}";
  142. byte[] buffer = Encoding.Default.GetBytes(str);
  143. MD5 md5 = MD5.Create();
  144. byte[] bufferNew = md5.ComputeHash(buffer);
  145. string strNew = "";
  146. for (int i = 0; i < bufferNew.Length; i++)
  147. {
  148. strNew += bufferNew[i].ToString("x2");
  149. }
  150. return strNew;
  151. }
  152. catch (Exception ex)
  153. {
  154. errorinfo = ex.Message.ToString();
  155. return "";
  156. }
  157. }
  158. }
  159. }