找传奇、传世资源到传世资源站!

C# 检查本地硬盘序列号

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

C# 利用diskid32.exe 检查本地硬盘序列号 CPU 以及MAC地址
from clipboard using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Drawing;using System.Linq;using System.Management;using System.Net.NetworkInformation;using System.Runtime.InteropServices;using System.Text;using System.Windows.Forms;namespace 检查本地硬盘序列号{ public partial class Form1 : Form { /// <summary> /// 获取磁盘逻辑序列号 /// </summary> /// <param name="lpRootPathName">欲获取信息的那个卷的根路径 </param> /// <param name="lpVolumeNameBuffer">用于装载卷名(卷标)的一个字串 </param> /// <param name="nVolumeNameSize">lpVolumeNameBuffer字串的长度 </param> /// <param name="lpVolumeSerialNumber">用于装载磁盘卷序列号的变量 </param> /// <param name="lpMaximumComponentLength">指定一个变量,用于装载文件名每一部分的长度。例如,在“c:\component1\component2.ext”的情况下,它就代表component1或component2名称的长度 .</param> /// <param name="lpFileSystemFlags">用于装载一个或多个二进制位标志的变量。对这些标志位的解释如下: /// FS_CASE_IS_PRESERVED 文件名的大小写记录于文件系统 /// FS_CASE_SENSITIVE 文件名要区分大小写 /// FS_UNICODE_STORED_ON_DISK 文件名保存为Unicode格式 /// FS_PERSISTANT_ACLS 文件系统支持文件的访问控制列表(ACL)安全机制 /// FS_FILE_COMPRESSION 文件系统支持逐文件的进行文件压缩 /// FS_VOL_IS_COMPRESSED 整个磁盘卷都是压缩的 </param> /// <param name="lpFileSystemNameBuffer">指定一个缓冲区,用于装载文件系统的名称(如FAT,NTFS以及其他)</param> /// <param name="nFileSystemNameSize">lpFileSystemNameBuffer字串的长度</param> /// <returns></returns> [DllImport("Kernel32.dll", CharSet = CharSet.Auto)] public static extern bool GetVolumeInformation( string lpRootPathName, string lpVolumeNameBuffer, int nVolumeNameSize, ref int lpVolumeSerialNumber, int lpMaximumComponentLength, int lpFileSystemFlags, string lpFileSystemNameBuffer, int nFileSystemNameSize); public Form1() { InitializeComponent(); label4.Text = CpuID(); List<string> disklist = GetHardDiskIDList(); if (disklist != null) for (int i = 0; i < disklist.Count; i ) { richTextBox2.Text = disklist[i] "\r\n"; } List<string> maclist = GetMacList(); if(maclist !=null) for (int i = 0; i < maclist.Count; i ) { richTextBox3.Text = maclist[i] "\r\n"; } } /// <summary> /// /// </summary> private object _hardDiskIDThrObj = new object(); /// <summary> /// /// </summary> private List<string> _hdSerList = null; public List<string> GetHardDiskIDList() { lock (_hardDiskIDThrObj) { if (_hdSerList != null) { return _hdSerList.ToList(); } string input = "diskid32"; string output = string.Empty; using (Process p = new Process()) { p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动 p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息 p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息 p.StartInfo.RedirectStandardError = true;//重定向标准错误输出 p.StartInfo.CreateNoWindow = true;//不显示程序窗口 p.StartInfo.ErrorDialog = false; p.Start();//启动程序 //向cmd窗口发送输入信息 p.StandardInput.WriteLine(input "&exit"); p.StandardInput.AutoFlush = true; //p.StandardInput.WriteLine("exit"); //向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死 //同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令 //获取cmd窗口的输出信息 output = p.StandardOutput.ReadToEnd(); p.WaitForExit(100);//等待程序执行完退出进程 p.Close(); } if (output != string.Empty) { this.richTextBox1.Text = output.ToString(); List<string> hdSerList = new List<string>(); string val = string.Empty; int index = 0; string[] lines = output.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (string line in lines) { if (line.IndexOf("Serial Number") < 0) { continue; } index = line.IndexOf(':'); if (index < 0) { index = line.IndexOf('='); if (index < 0) { continue; } } if (line.Length < index 1) { continue; } val = line.Substring(index 1).Trim().Trim('[').Trim(']').Trim(); if (val == "0000_0000_0000_0000." || val == "00000_00_000000_00.0") { continue; } if (!string.IsNullOrEmpty(val) && !hdSerList.Contains(val)) { hdSerList.Add(val); } } if (hdSerList.Count < 1) { hdSerList.Add(GetdiskID()); } _hdSerList = hdSerList; } else { _hdSerList = new List<string>(); _hdSerList.Add(GetdiskID()); } return _hdSerList.ToList(); } } /// <summary> /// 获取硬盘ID /// </summary> /// <returns></returns> private string GetdiskID() { try { const int MAX_FILENAME_LEN = 256; int retVal = 0; int a = 0; int b = 0; string str1 = null; string str2 = null; GetVolumeInformation( @"C:\", str1, MAX_FILENAME_LEN, ref retVal, a, b, str2, MAX_FILENAME_LEN); return retVal.ToString("X2"); } catch { return "00000000"; } } private static string _cpuID = string.Empty; private static object _cpuIDThrObj = new object(); /// <summary> /// 取CPU编号 /// </summary> /// <param name="refresh">是否重新获取,默认否</param> /// <returns></returns> public static string CpuID(bool refresh = false) { lock (_cpuIDThrObj) { try { if (_cpuID != string.Empty && !refresh) { return _cpuID; } ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { _cpuID = mo.Properties["ProcessorId"].Value.ToString(); return _cpuID; } _cpuID = string.Empty; return _cpuID; } catch { _cpuID = string.Empty; return _cpuID; } } } private static List<string> _macList = null; /// <summary> /// 获取本地Mac地址列表 /// </summary> /// <returns></returns> public static List<string> GetMacList() { if (_macList == null) { List<string> maclist = new List<string>(); try { NetworkInterface[] niArray = NetworkInterface.GetAllNetworkInterfaces(); if (niArray.Length < 1) { maclist = new List<string>(); } else { foreach (NetworkInterface entity in niArray) { if (!entity.SupportsMulticast) continue; string mac = entity.GetPhysicalAddress().GetAddressBytes().ToHexString(); if (string.IsNullOrEmpty(mac)) continue; maclist.Add(mac); } } //return niArray.Select(p => p.GetPhysicalAddress().GetAddressBytes().ToHexString()).ToArray(); } catch { maclist = new List<string>(); } _macList = maclist; } return _macList.ToList(); } } public static class Extensions { /// <summary> /// 将byte转换为Hex格式字符串 /// </summary> /// <param name="source">源</param> /// <param name="spl">分隔符</param> /// <returns></returns> public static string ToHexString(this byte[] source, string spl = "") { if (source == null || source.Length < 1) return string.Empty; spl = spl == null ? string.Empty : spl; StringBuilder sb = new StringBuilder(source.Length * (2 spl.Length)); for (int i = 0; i < source.Length; i ) { sb.Append(source[i].ToString("X2")); if (i < source.Length - 1) { sb.Append(spl); } } return sb.ToString(); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复