AppTimeSpanClass.cs 10.5 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;//提供对大量管理信息和管理事件集合的访问,这些信息和事件是与根据 Windows 管理规范 (WMI) 结构对系统、设备和应用程序设置检测点有关的。
using System.Security.Cryptography;
using Microsoft.Win32;//命名空间提供两种类型的类:处理由操作系统引发的事件的类和操作系统注册表的类。


namespace MyClassLib
{
    public  class AppTimeSpanClass
    {

        string[] strCertificate = new string[100] { "f1ae15c5-7fd9-43f4-aa2e-b3742d9e103a", "6afa7b49-355c-44bc-ae4d-6a3b25915b6b", "5ae2494e-8d39-4efc-a40d-a022c50b4d5a", "8780e025-e37f-4b43-bb58-646043e4dd72", "a51de268-78bb-4abe-b8ba-a7a15b1ee9d0", "d7ff2554-8e40-4bf0-8043-4ebde74fc87f", "439f183a-ca41-4eae-a514-2bfea953712a", "5c5cd7d3-3a5c-472b-8c74-bb409e4ab196", "23ff5adf-54b0-4daf-b98c-dd31a4374f7b", "970e51f5-c8b4-4d6d-944c-7be861a4295b", "fa01bbf5-2118-46ad-993b-69954d5879b7", "741f808c-9230-46f6-8620-a793639ee80e", "f70e5eec-7c77-4434-9bf4-6e8b578a9891", "c1fd6068-26fe-45ba-8f26-15eb6638aeaa", "c6ac40a2-f505-4740-a4cb-e22769b061dc", "66711cdc-cf4c-40bf-aed6-835e6f0a1a2a", "aca8f505-618e-453d-8700-ee7d761b2914", "8e3e6934-7d39-4beb-ae02-51631ee12740", "94869bac-63b6-4e46-92eb-5cd8a4911cba", "7683e2ad-bf71-47a4-964a-47ac38f2958b", "c189bb5c-afad-4d35-909f-c8aa5381aac0", "dbc887d2-bac6-47f5-9f28-03b5d663f56f", "3c088532-fb3c-4e7f-b71c-44272a534f24", "c83104a0-68af-4d65-a4a4-c27ad3f99454", "c54d6a99-44c0-41e3-a2a6-ce26ffc70d8d", "bdb6f6e7-454c-43e5-a56d-cea84919a787", "ef167ef5-decf-4847-932b-c35536a5af3b", "0605b70b-fdf3-4f63-880f-d5fc70b6fb13", "b2304914-b7e9-4d1f-a179-44f1b14d1a7d", "7d43e239-109e-44f2-99fa-03d97fe5b9f8", "109f5316-96f8-45db-a02c-332337cc572a", "b20f213a-ed47-44e2-a31d-20bb5b5925ff", "8e3d1bb5-380c-4caf-9114-335b728ac6df", "03bc8534-ce5b-42e1-912b-09b04215462b", "76e1507c-c1f2-45f9-a3ac-589e73ef02e7", "a3f491e6-2964-4cfc-8f16-ad90e4d16a45", "5f6ecbe7-fb82-4fa2-b814-d6a9ca38db5d", "af454ba0-12ad-418c-83b9-578d50e917df", "12fe86d1-8c20-42f3-a780-45f38b83b315", "127ce629-d8f8-4ede-bf31-820d3a3226e0", "4d017310-5fab-43d9-8181-ea43e3a09493", "a25d215f-fe6f-45cc-8ffe-8d9e7c9dc29e", "17df786f-b88e-41c9-91bb-ab80f0cd4507", "f3848f41-fed3-49db-baf9-2bf67163f5db", "920f9d07-3d2c-4c5b-94bd-ce927bdd2e7c", "93c19653-cbbf-4850-b342-b07051784500", "86687131-5ac7-4526-99eb-c39a85c2ec06", "218d9628-e258-4a1b-a11a-b02c39959fa1", "032c678f-1c39-43a9-9eec-b4dbf9e604d9", "4abba35a-dd51-4e98-995d-8bfc021dd068", "dd8c76f8-2711-45ea-be16-0f670e711828", "5605942b-bb3e-4f6f-8c7a-20052fc3b41f", "0d6195b2-c06d-4e32-b3af-d6cfb4503d60", "32b8631d-80b8-4569-ae28-4e90923652d6", "4b43767e-db6e-45b6-93dc-385377eadf38", "d2ee2a26-a0ab-4a06-bea9-06c8e9f439f1", "843d577f-26d6-4d1c-a512-df4f80042ae6", "298b2a34-388c-4ed0-ad9e-841233f14b20", "b3d9e0a2-f415-46b2-a97e-e9abaecff893", "439814eb-caa4-439b-af7a-fec0c912710e", "7dc84f0d-698a-49bc-973e-a956dd1618c4", "901419b9-dd6b-4a53-9d78-9df3f23c485a", "89631f39-38e1-4159-8628-f14aba80b98b", "922a14a9-7a8e-43bf-be85-961581396652", "422bb765-85c0-4515-ab3e-cc7722adfe83", "0e5ef74f-16e0-412b-9200-d718404a75b4", "bcfdae3e-74f5-4972-a170-2abba7d6cba8", "6a67699a-8309-4e24-bc66-1963be589d0c", "b06f8237-6010-47a8-bb5a-0ab0546cffe6", "e695c2b6-b916-46a4-b597-cbd2f6bfc3a2", "9fc1b17d-777f-49d0-9122-1b87d8b4b9a2", "3cae608e-e71f-4a89-8056-4369539cd704", "85902734-e32f-40d9-8cb0-d75e58fedac7", "7737cfac-4f8a-4f3e-93c5-6836f45d8783", "c1282825-6745-4166-93e4-244d35fe9a35", "62578516-36d2-4635-b5cf-53855a74680d", "eeb884f7-b3cc-4866-8444-fb68584fd058", "4a12d60c-a419-45b3-a1d9-679cceb53a32", "6a4849d9-af40-43d9-8f65-e0ded3636043", "fd31eafa-faa5-4171-9cef-8a7e5b55f4c1", "5b3da8d8-f0dc-499a-84c3-6a629d4c776d", "30c74499-44ca-4aef-9e56-12e9ad508881", "a780b49c-fa51-42d2-b80e-f8d817634cc8", "ce785100-7733-42bb-9517-31b182037768", "8bc62fd1-0e38-415d-ae9a-aa2ee5da2f52", "fb888611-c6b6-4faa-8a0e-0a4ef5af2550", "dae319e2-2374-4da2-8f5f-ee9a8e3042e8", "a4e3092f-4551-4830-a184-a85cac4053e5", "37bf7914-6086-4368-a0e6-fd1e64b34208", "ff2db180-d273-438c-acc3-40560f717ca6", "41eb4663-a987-4179-a9be-49a4dfa8f841", "0b05b402-ae1a-40ff-8523-e08c06621554", "af413c0b-b81c-41dc-a9eb-6ddf4f4238ab", "d39bfc4c-3105-428d-83ca-283199d93d61", "bbf3a0af-c750-4354-9c99-f6bffc6a0938", "b1bd6703-c945-4838-ad8d-99d731e88265", "9a986a03-307e-47c5-a89d-760d77af2316", "d6675c0d-c1c4-468c-bce5-df27e95bb0f1", "d63ec737-1845-465a-bae9-6257f44ad9eb", "d85fc859-8964-4377-9927-3461972ba767" };
       
        public static int InitRegedit()
        { 
            //检查注册表
            string SericalNumber = ReadSetting("", "SerialNumber", "-1");//读取注册表,检查是否注册 -1为未注册
            if (SericalNumber == "-1")
            {
                return 1;
            }
            //比较CPU ID
            string CpuID = GetSoftEndDateAllCpuId(1, SericalNumber);//从注册表读取CPU ID
            string CpuIDThis = GetCPUId();//获取本机的CPU ID
            if (CpuID != CpuIDThis)
            {
                return 2;
            }
            //比较时间
            string NowDate = AppTimeSpanClass.GetNowDate();
            string EndDate = AppTimeSpanClass.GetSoftEndDateAllCpuId(0, SericalNumber);
            if (Convert.ToInt32(EndDate) - Convert.ToInt32(NowDate) < 0)
            {
                return 3;
            }
            return 0;
        }

        public static int InitRegedit(string Section,string Key,string DefaultValue)
        {
            //检查注册表
            string SericalNumber = ReadSetting(Section, Key, DefaultValue);//读取注册表,检查是否注册 -1为未注册
            if (SericalNumber == "-1")
            {
                return 1;
            }
            //比较CPU ID
            string CpuID = GetSoftEndDateAllCpuId(1, SericalNumber);//从注册表读取CPU ID
            string CpuIDThis = GetCPUId();//获取本机的CPU ID
            if (CpuID != CpuIDThis)
            {
                return 2;
            }
            //比较时间
            string NowDate = AppTimeSpanClass.GetNowDate();
            string EndDate = AppTimeSpanClass.GetSoftEndDateAllCpuId(0, SericalNumber);
            if (Convert.ToInt32(EndDate) - Convert.ToInt32(NowDate) < 0)
            {
                return 3;
            }
            return 0;
        }

        /// <summary>
        /// 获区CPU ID
        /// </summary>
        /// <returns></returns>
        public static string GetCPUId()
        {
            ManagementClass ms = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = ms.GetInstances();
            string strCpuID = null;
            foreach (ManagementObject mo in moc)
            {
                strCpuID = mo.Properties["ProcessorId"].Value.ToString();
                break;
            }
            return strCpuID;
        }
        /// <summary>
        /// 获取当前时间
        /// </summary>
        /// <returns></returns>
        public static string GetNowDate()
        {
            string NowDate = DateTime.Now.ToString("yyyyMMdd");
            return NowDate;
        }

        /// <summary>
        /// 生成序列号
        /// </summary>
        /// <returns></returns>
        public static string CreatSerialNumber()
        {
            string SerialNumber = GetCPUId() + "-" + "20150915";
            return SerialNumber;
        }
        /// <summary>
        /// 生成序列号
        /// </summary>
        /// <param name="yyyMMdd">日期格式</param>
        /// <returns></returns>
        public static string CreatSerialNumber(string yyyMMdd)
        {
            string SerialNumber = GetCPUId() + "-" + yyyMMdd;
            return SerialNumber;
        }
        /// <summary>
        /// 获取软件运行的最终日期和CPU ID
        /// 0:表示得到上次或者开始时间 1:得到CPU的ID
        /// </summary>
        /// <param name="i"></param>
        /// <param name="SerialNumber"></param>
        /// <returns></returns>
        public static string GetSoftEndDateAllCpuId(int i, string SerialNumber)
        {
            if (i == 1)
            {
                string CpuID = SerialNumber.Substring(0, SerialNumber.LastIndexOf('-'));
                return CpuID;
            }
            else if (i == 0)
            {
                string dateTime = SerialNumber.Substring(SerialNumber.LastIndexOf('-') + 1);
                return dateTime;
            }
            else
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// 写入注册表
        /// </summary>
        /// <param name="Section">Path</param>
        /// <param name="Key">Name</param>
        /// <param name="Setting">Value</param>
        public static void WriteSetting(string Section, string Key, string Setting)
        {
            //string strPath1 = Section;
            RegistryKey key1 = Registry.CurrentUser.CreateSubKey(Section);
           // RegistryKey key1 = Registry.CurrentUser.CreateSubKey("Software\\SunEast\\SoftDepartment");
            //Registry.LocalMachine.CreateSubKey("Software\\SunEast\\SoftDepartment");
            if (key1 == null)
            {
                return;
            }
            try
            {
                key1.SetValue(Key, EncryptUtils.DESEncrypt(Setting,EncryptUtils.Key,EncryptUtils.Iv));
            }
            catch (Exception ex)
            {
                return;
            }
            finally
            {
                key1.Close();
            }
        }
        /// <summary>
        /// 读取注册表
        /// </summary>
        /// <param name="Section">Path</param>
        /// <param name="Key">Name</param>
        /// <param name="Setting">Value</param>
        public static string ReadSetting(string Section,string Key,string Default)
        {
            if (Default == null)
            {
                Default = "-1";
            }
            RegistryKey key1 = Registry.CurrentUser.OpenSubKey(Section);
            if (key1 != null)
            {
                object ob1 = key1.GetValue(Key, Default);
                key1.Close();
                if (!(ob1 is string))
                {
                    return "-1";
                }
                string str2 = ob1.ToString();
                str2 = EncryptUtils.DESDecrypt(str2, EncryptUtils.Key, EncryptUtils.Iv);
                return str2;
            }
            return Default;
        }
        //鼠标相对于屏幕的坐标Point p1 = MousePosition;//鼠标相对于窗体的坐标Point p2 = this.PointToClient(p1);


    }
}