• C# WIN 生成机器码


    内容归档,不多赘述。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Management;
    using System.Security.Cryptography;
    using System.Text;
    
    namespace MachineCode
    {
        public class MachineCode
        {
    
            public static string GetMachineCode()
            {
                try
                {
                    string rs = ToReverse(To_md5(GetCpuInfo() + GetMoAddress() + GetHDid()));
                    return rs;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
    
            ///   <summary>
            ///   获取cpu序列号    
            ///   </summary>
            ///   <returns> string </returns>
            private static string GetCpuInfo()
            {
                string cpuInfo = " ";
                using (ManagementClass cimobject = new ManagementClass("Win32_Processor"))
                {
                    ManagementObjectCollection moc = cimobject.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                        mo.Dispose();
                    }
                }
                return cpuInfo.ToString();
            }
    
            ///   <summary>
            ///   获取硬盘ID    
            ///   </summary>
            ///   <returns> string </returns>
            private static string GetHDid()
            {
                string HDid = " ";
                using (ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive"))
                {
                    ManagementObjectCollection moc1 = cimobject1.GetInstances();
                    foreach (ManagementObject mo in moc1)
                    {
                        HDid = (string)mo.Properties["Model"].Value;
                        mo.Dispose();
                    }
                }
                return HDid.ToString();
            }
    
            ///   <summary>
            ///   获取网卡硬件地址
            ///   </summary>
            ///   <returns> string </returns>
            private static string GetMoAddress()
            {
                string MoAddress = " ";
                using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                {
                    ManagementObjectCollection moc2 = mc.GetInstances();
                    foreach (ManagementObject mo in moc2)
                    {
                        if ((bool)mo["IPEnabled"] == true)
                            MoAddress = mo["MacAddress"].ToString();
                        mo.Dispose();
                    }
                }
                return MoAddress.ToString();
            }
    
    
    
    
            /// <summary>
            /// 根据字符串生成MD5
            /// </summary>
            /// <param name="str">原字符串</param>
            /// <param name="up">是否大小写</param>
            /// <returns>生成后的MD5</returns>
            public static string To_md5(string str, bool up = false)
            {
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                string t2 =
                BitConverter.ToString(md5.ComputeHash(
                UTF8Encoding.Default.GetBytes(str)));
                t2 = t2.Replace("-", "");
                if (!up)
                    return t2.ToLower();
                else
                    return t2.ToUpper();
            }
    
    
            public static string ToReverse(string str)
            {
                return new string(str.ToCharArray().Reverse().ToArray());
            }
        }
    }
  • 相关阅读:
    CodeBlocks 中fopen函数不支持命令 “r”
    【转载】分享一些Qt学习资源,欢迎下载
    【转载】知乎答案----孙志岗----Google 发布了程序员养成指南,国内互联网巨头是否也有类似的指南和课程推荐
    【转载】谷歌公司推荐的程序员必修课(英文教程)
    【转载】张逸--ThoughtWorks(中国)程序员读书雷达
    在windows环境下,为什么要用Notepad++编辑?
    【转载】池建强--趣谈个人建站
    JAVA入门第二季 第一章 类和对象
    CAP理论总结
    分布式通信方式之消息队列之RocketMQ
  • 原文地址:https://www.cnblogs.com/myron1024/p/13896375.html
Copyright © 2020-2023  润新知