实例介绍
【实例截图】
【核心代码】
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(); } } }
标签: 检查本地硬盘序列号
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论