最近做的项目,有个需求需要软件注册激活后才能使用,激活这种事情么,从来没做过,网上找找大多数说法都是绑定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#获取windows下用于标志当前电脑的唯一性编号相关推荐

  1. Python3获取Windows下Chrome 90版本的Cookie值

    Python3获取Windows下Chrome 90版本的Cookie值 文章目录 Python3获取Windows下Chrome 90版本的Cookie值 前言 一.AES_GSM加密方式 二.DP ...

  2. linux和windows下分别如何查看电脑是32位的还是64位?

    WINDOWS下查看的方法: 方法一. 在开始→运行中输入"winver",如果您的系统是64位的,会明确标示出"x64 edition". 方法二.(推荐) ...

  3. C#获取Windows下光标位置(转)

    使用C#获取光标相对于显示器屏幕的位置: 方式一: 1 [csharp] view plaincopyprint? 2 using System; 3 using System.Drawing; 4 ...

  4. Qt5获取windows下除去任务栏后的屏幕高度

    头文件添加: #include "windows.h" 引用windows API获取除去任务栏后的高度和宽度: int width = GetSystemMetrics(SM_C ...

  5. windows下在一台电脑上配置多个git账号

    假设环境 配置账号一 生成ssh密钥 github网站配置 在相应的.ssh目录下会生成名为id_rsa私钥文件和id_rsa.pub公钥文件, 打开id_rsa.pub公钥文件,复制里面的公钥添加到 ...

  6. Qt笔记-获取Windows下目前运行的进程信息

    目录 基本概念 代码与实例 源码下载 基本概念 知识点如下: CreateToolhelp32Snapshot    获取当前系统进程快照 void Thread::getSnapshot() {HA ...

  7. windows下通过uiAutomation技术获取ui元素

    最近接个需求,要求获取 windows 下 ui 元素,经一番搜索后了解到可通过工具 UISpy.exe 或 inspect.exe 来进行查看.以软件 davinci resolve 为例: 右侧即 ...

  8. Windows下创建进程-CreateProcess()

    原文地址:https://blog.csdn.net/yuyan987/article/details/78644922 函数说明: CreateProcess是Windows下用于创建进程的API函 ...

  9. windows下linux下socket编程区别

    1. 头文件 windows下winsock.h或winsock2.h linux下netinet/in.h(大部分都在这儿),unistd.h(close函数在这儿),sys/socket.h(在i ...

最新文章

  1. mt4指标最精准组合指标_股市最赚钱的黄金指标组合:KDJ+MACD指标的配合使用,助于买在低点卖在高点!...
  2. tpp letter
  3. 线程间操作无效: 从不是创建控件“button2”的线程访问它
  4. 初识Mysql(part19)--我需要知道的3条Mysql语句之组合查询
  5. 华为EC169 3G卡在Win7下的安装
  6. 防止表单重复提交的解决方案整理
  7. vue路由跳转动画_Vue路由跳转动画
  8. Python 数据分析实战:经典的同期群分析
  9. mysql判断后执行查询语句吗_如何看mysql执行的sql语句
  10. 安装Python和Anaconda
  11. Springboot应用缓存实践之:Ehcache加持
  12. 101个著名的管理学及心理学效应
  13. 41.clip-path 滚动特效
  14. 201709-2公共钥匙盒
  15. spring中<tx:annotation-driven>标签转为注解@EnableTransactionManagement
  16. 关于2018后新款 Mac增加T2安全芯片造成无法U盘启动解决办法
  17. 【小程序】视图与逻辑
  18. muduo源码学习 Day03
  19. 信息学奥赛一本通1267:【例9.11】01背包问题(二维dp与滚动数组优化)
  20. 电视剧《龙虎人生》剧照

热门文章

  1. Fiddler 4 前期配置指南——实现手机代理
  2. BMC固件漏洞导致OT和IoT设备易受远程攻击
  3. 用一句话概括计算机信息管理,一句话概括ERP
  4. vb窗体left、top、width、height,scaleleft、scaletop、scalewidth、scaleheith区别。
  5. MybatisPlus分页查询
  6. null id in entry (don't flush the Session after an exception occurs)的错误
  7. 谷歌开源基于 ML 的手部跟踪算法
  8. 风控建模六:变量相关性分析及筛选方法
  9. 在Cent OS云服务器上部署基于TP5后端代码踩坑记录_艾孜尔江撰
  10. 分析Vue双向数据绑定原理以及简单实现MVVM