using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading; using System.Windows.Forms; namespace MachineFrm { public partial class SystemInfoFrm : Form { static string valsystem ; static string valmac ; static string valcpu ; static string valboard; public SystemInfoFrm() { InitializeComponent(); } private void btnGet_Click(object sender, EventArgs e) { btnGet.Enabled = false; Thread.Sleep(3000); GetMachineInfo(); btnGet.Enabled = true; MessageBox.Show("已经重新获取了机器信息!"); } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } private void SystemInfoFrm_Load(object sender, EventArgs e) { string a = GetKey(); string b = GetKey2(); GetMachineInfo(); } public string GetKey() { string errorinfo = string.Empty; string val1 = "BFEBFBFF000906E9"; string val2 = "/3B2LKQ2/CNFCW0086402KS/"; return GetKeyInfo("",val1,val2,ref errorinfo); } public string GetKey2() { string errorinfo = string.Empty; string val1 = "BFEBFBFF000306C3"; string val2 = "160162955501177"; return GetKeyInfo("", val1, val2, ref errorinfo); } public void GetMachineInfo() { try { string a = Guid.NewGuid().ToString(); valsystem = MachineInfo.GetSystemName() + "-" + MachineInfo.GetSystemType(); valmac = MachineInfo.GetMacAddress(); valcpu = MachineInfo.GetCpuID(); valboard = MachineInfo.GetMotherBoardID(); txtSystem.Text = valsystem; txtMac.Text = valmac; txtCpu.Text = valcpu; txtBoard.Text = valboard; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void btnExport_Click(object sender, EventArgs e) { try { FolderBrowserDialog ofd = new FolderBrowserDialog(); if (ofd.ShowDialog() == DialogResult.OK) { string targetPath = ofd.SelectedPath; string filename = targetPath + Path.DirectorySeparatorChar + "SystemInfoFrm" + ".txt"; if (!Directory.Exists(targetPath))//验证路径是否存在 { Directory.CreateDirectory(targetPath); //不存在则创建 } FileStream fs; StreamWriter sw; if (File.Exists(filename)) //验证文件是否存在,有则追加,无则创建 { fs = new FileStream(filename, FileMode.Append, FileAccess.Write); } else { fs = new FileStream(filename, FileMode.Create, FileAccess.Write); } sw = new StreamWriter(fs); //sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") ); sw.WriteLine("[SystemInfoFrm]获取机器基本信息。time:" + DateTime.Now.ToString()); //sw.WriteLine("------------------------------------------------------------------"); sw.WriteLine($"操作系统:{valsystem}"); sw.WriteLine($"mac地址:{valmac}"); sw.WriteLine($"cpu序号:{valcpu}"); sw.WriteLine($"主板序号:{valboard}"); //sw.WriteLine("------------------------------------------------------------------"); sw.Close(); fs.Close(); } MessageBox.Show("已成功导出SystemInfoFrm.txt文件!"); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private string GetKeyInfo(string keyone, string keytwo, string keythree, ref string errorinfo) { try { if (keyone == "unknown" || string.IsNullOrEmpty(keyone)) { //errorinfo = "GetMacAddress[unknown] "; //return ""; keyone = ""; } if (keytwo == "unknown") { errorinfo = "GetCpuID[unknown] "; return ""; } if (keythree == "unknown") { errorinfo = "GetMotherBoardID[unknown] "; return ""; } string k1 = "9d6a61dc-d5cf-4199-81b0-d4bb3ee6f613"; string k2 = "b41f7f33-28a8-48fa-876a-87892f95d7f9"; string str = $"{keyone}{k1}{keytwo}{k2}{keythree}"; byte[] buffer = Encoding.Default.GetBytes(str); MD5 md5 = MD5.Create(); byte[] bufferNew = md5.ComputeHash(buffer); string strNew = ""; for (int i = 0; i < bufferNew.Length; i++) { strNew += bufferNew[i].ToString("x2"); } return strNew; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return ""; } } } }