C#获取windows下用于标志当前电脑的唯一性编号

最近做的项目,有个需求需要软件注册激活后才能使用,激活这种事情么,从来没做过,网上找找大多数说法都是绑定PC机器的物理硬件,推荐用SmBIOSUUID,获取不到的情况下再用其它硬件序列号组合,但找了一圈没找到现成的C#代码获取相关ID的文章,后来想了下,既然都是cmd指令,那我C#里面直接调用cmd执行指令不就行了,于是就有了下面的代码

    public class ComputerUniqueHelper
    {
        public static string GetComputerUUID()
        {
            var uuid = GetSmBIOSUUID();
            if (string.IsNullOrWhiteSpace(uuid))
            {
                var cpuID = GetCPUID();
                var biosSerialNumber = GetBIOSSerialNumber();
                var diskSerialNumber = GetDiskDriveSerialNumber();
                uuid = $"{cpuID}__{biosSerialNumber}__{diskSerialNumber}";
            }
            return uuid;
        }

        public static string? GetSmBIOSUUID()
        {
            var cmd = "wmic csproduct get UUID";
            return ExecuteCMD(cmd, output =>
            {
                string? uuid = GetTextAfterSpecialText(output, "UUID");
                if (uuid == "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF")
                {
                    uuid = null;
                }
                return uuid;
            });
        }
        public static string? GetCPUID()
        {
            var cmd = "wmic cpu get processorid";
            return ExecuteCMD(cmd, output =>
            {
                var cpuid = GetTextAfterSpecialText(output, "ProcessorId");
                return cpuid;
            });
        }
        public static string? GetBIOSSerialNumber()
        {
            var cmd = "wmic bios get serialnumber";
            return ExecuteCMD(cmd, output =>
            {
                var serialNumber = GetTextAfterSpecialText(output, "SerialNumber");
                return serialNumber;
            });
        }
        public static string? GetDiskDriveSerialNumber()
        {
            var cmd = "wmic diskdrive get serialnumber";
            return ExecuteCMD(cmd, output =>
            {
                var serialNumber = GetTextAfterSpecialText(output, "SerialNumber");
                return serialNumber;
            });
        }
        private static string? GetTextAfterSpecialText(string fullText, string specialText)
        {
            if (string.IsNullOrWhiteSpace(fullText) || string.IsNullOrWhiteSpace(specialText))
            {
                return null;
            }
            string? lastText = null;
            var idx = fullText.LastIndexOf(specialText);
            if (idx > 0)
            {
                lastText = fullText.Substring(idx + specialText.Length).Trim();
            }
            return lastText;
        }
        private static string? ExecuteCMD(string cmd, Func<string, string?> filterFunc)
        {
            using var process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
            process.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
            process.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            process.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            process.StartInfo.CreateNoWindow = true;//不显示程序窗口
            process.Start();//启动程序
            process.StandardInput.WriteLine(cmd + " &exit");
            process.StandardInput.AutoFlush = true;
            //获取cmd窗口的输出信息
            var output = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            process.Close();
            return filterFunc(output);
        }
    }

相关资料
[Windows] 获取设备唯一标识
获取机器序列号的方法
C#程序调用cmd.exe执行命令

你可能感兴趣的:(C#,c#)