| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 | using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Management;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Xml;namespace RegistrationAuthorization{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            SaveFileDialog sfd = new SaveFileDialog();            sfd.Filter = "(*.txt)|*.txt|(*.*)|*.*";            if (sfd.ShowDialog() == DialogResult.OK)            {                IDictionary<String, List<String>> infos = new Dictionary<String, List<String>>();                infos.Add("ID", new List<string>() { getCpu(), GetDiskVolumeSerialNumber() });                infos.Add("Time", new List<string>() { DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString(), DateTime.Now.Day.ToString() });                SaveXML(infos, sfd.FileName);                MessageBox.Show("Save complete");            }        }        // 获得CPU的序列号        static string getCpu()        {            string strCpu = null;            ManagementClass myCpu = new ManagementClass("win32_Processor");            var myCpuConnection = myCpu.GetInstances();            foreach (ManagementObject myObject in myCpuConnection)            {                strCpu = myObject.Properties["Processorid"].Value.ToString();                break;            }            return strCpu;        }        // 取得设备硬盘的卷标号        static string GetDiskVolumeSerialNumber()        {            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");            ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");            disk.Get();            return disk.GetPropertyValue("VolumeSerialNumber").ToString();        }        public static void SaveXML(IDictionary<String, List<String>> infos, string address)        {            if (infos == null || infos.Count == 0)            {                return;            }            XmlDocument xmlDoc = new XmlDocument();            XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);            xmlDoc.AppendChild(dec);            XmlElement _infos = xmlDoc.CreateElement("infos");            foreach (KeyValuePair<String, List<String>> item in infos)            {                XmlElement info = xmlDoc.CreateElement("info");                XmlElement name = xmlDoc.CreateElement("ID");                name.InnerText = item.Key;                info.AppendChild(name);                XmlNode filelist = xmlDoc.CreateElement("filelist");                info.AppendChild(filelist);                foreach (String number in item.Value)                {                    XmlElement filed = xmlDoc.CreateElement("filed");                    filed.InnerText = number;                    filelist.AppendChild(filed);                }                _infos.AppendChild(info);            }            xmlDoc.AppendChild(_infos);            xmlDoc.Save(address);        }        private void Form1_Load(object sender, EventArgs e)        {            string CPUID, DiskID;            CPUID = getCpu();            DiskID = GetDiskVolumeSerialNumber();            var ID = ReadXML("./Config/SysData/RegistrationProofreading.txt");            List<string> list_time = new List<string>();            List<string> list_str = new List<string>();            ID.TryGetValue("ID", out list_str);            ID.TryGetValue("Time", out list_time);            ID.TryGetValue("ID", out list_str);            ID.TryGetValue("Time", out list_time);            string setCPU = ConvertString(list_str[0]);            string setDisk = ConvertString(list_str[1]);            if (CPUID != setCPU || DiskID != setDisk)            {                label1.Text = "缺少授权!";                label2.Visible = false;            }            else            {                label2.Visible = true;                DateTime dt = DateTime.ParseExact(ConvertString(list_time[0]) + ConvertString(list_time[1]) + ConvertString(list_time[2]) + "235959", "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);                if (DateTime.Now > dt)                {                    label1.Text = "授权过期!";                }                else                {                    label1.Text = "授权已激活";                }                label2.Text = "有效期至:" + ConvertString(list_time[0]) + "-" + ConvertString(list_time[1]) + "-" + ConvertString(list_time[2]);            }                    }        public static IDictionary<String, List<String>> ReadXML(string address)        {            IDictionary<String, List<String>> infos = new Dictionary<String, List<String>>();            if (File.Exists(address))            {                XmlDocument xmlDoc = new XmlDocument();                xmlDoc.Load(address);                XmlNode xn = xmlDoc.SelectSingleNode("infos");                XmlNodeList xnl = xn.ChildNodes;                foreach (XmlNode xnf in xnl)                {                    XmlElement xe = (XmlElement)xnf;                    XmlNode nameNode = xe.SelectSingleNode("ID");                    string name = nameNode.InnerText;                    Console.WriteLine(name);                    XmlNode filelist = xe.SelectSingleNode("filelist");                    List<String> list = new List<string>();                    foreach (XmlNode item in filelist.ChildNodes)                    {                        list.Add(item.InnerText);                    }                    infos.Add(name, list);                }            }            return infos;        }        private string ConvertString(string str)        {            string str_out;            string[] arr = str.Split(',');            byte[] be = new byte[arr.Count()];            for (int i = 0; i < arr.Count(); i++)            {                be[i] = Convert.ToByte(arr[i]);            }            str_out = Encoding.Unicode.GetString(be);            return str_out;        }    }}
 |