如何获取安装了Windows操作系统的硬盘序列号 [英] How can I get the serial number of hard disk where windows OS is installed
问题描述
我已经开发了一个使用硬盘序列号进行激活的应用程序但是当它来到使用多个硬盘的PC时它是一个问题所以我试图找到与os安装更加清晰的硬盘的序列号充当C盘的硬盘。我也试过下面的代码,但有时会返回D盘信息。
我尝试了什么:
I have developed an application that use hard disk serial number for activation but when its comes to pc that uses multiple hardisks its being a problem so iam trying to find the serial number of hard disk which comes with the os instalation more clearly the hard disk that act as C drive .I also tried the below code but it returns D drive information sometime.
What I have tried:
public string GetHardSerial()
{
ManagementObjectSearcher Finder = new ManagementObjectSearcher("Select * from Win32_OperatingSystem");
string Name = "";
string SerialNumber = "";
foreach (ManagementObject OS in Finder.Get()) Name = OS["Name"].ToString();
//Name = "Microsoft Windows XP Professional|C:\WINDOWS|\Device\Harddisk0\Partition1"
int ind = Name.IndexOf("Harddisk") + 8;
int HardIndex = Convert.ToInt16(Name.Substring(ind, 1));
Finder = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Index=" + HardIndex);
foreach (ManagementObject HardDisks in Finder.Get())
foreach (ManagementObject HardDisk in HardDisks.GetRelated("Win32_PhysicalMedia"))
SerialNumber = HardDisk["SerialNumber"].ToString();
// TextBox1.Text = Convert.ToString(Name);
// TextBox2.Text = Convert.ToString(SerialNumber);
return SerialNumber;
}
推荐答案
Windows并不总是安装在C:驱动器上。更重要的是,C:不代表磁盘,它代表磁盘上的分区。
如果您的代码获得D:驱动器的序列号,则表示安装在分区上的Windows已经分配了字母D.
为什么这是一个问题,因为你只需要一台计算机的结果是唯一的?无论返回什么序列,每次在同一台计算机上调用该函数时,只需返回相同的序列。
Windows is not always installed on C: drive. More than that, C: does not represent a disk, it represents a partition on a disk.
If your code gets the serial number of the D: drive, it means that Windows in installed on the partition which has been assigned the letter D.
Why is it an issue since you only need the result to be unique for a single computer? Whatever serial is returned, you only need the same serial to be returned everytime the function is called on the same computer.
您可能更好地处理处理器,而不是启动硬盘驱动器而不是序列号:硬盘寿命有限,而且它们比处理器更快失败。备份中恢复的映像在您的系统中不起作用,其中更改处理器几乎肯定是新的或不同的计算机(至少可能是新的RAM和主板)。
这些可能会有所帮助:
如何获取PC序列号使用C# [ ^ ]
处理器序列号 [ ^ ]
Rather than the boot up hard drive, you might be better handling the processor serial number instead: HDDs have a limited life, and they fail a lot quicker than processors do. A restored image from a backup would not work in your system, where changing the processor is almost certainly a new or different computer (and probably new RAM and motherboard at the very least).
These may help:
How to get PC serial number using C#[^]
Processor serial number[^]
如果您想获得安装Windows操作系统的驱动器号,请使用: Environment.GetEnviron mentVariable Method(System)| Microsoft Docs [ ^ ]
If you would like to get drive letter where Windows OS is installed, use this: Environment.GetEnvironmentVariable Method (System) | Microsoft Docs[^]
string windir = Environment.GetEnvironmentVariable("windir"); //or systemroot
Console.WriteLine("Windows OS is installed on: '{0}'", windir);
//displays: Windows OS is installed on 'C:\Windows'
//get drive info...
DriveInfo drive = DriveInfo.GetDrives()
.Where(d=>windir.Contains(d.Name))
.SingleOrDefault();
//further logic here....
详情请见:
环境.SpecialFolder枚举(系统)| Microsoft Docs [ ^ ]
Environment.GetLogicalDrives方法(系统)| Microsoft Docs [ ^ ]
如何检索REAL硬盘驱动器序列号 [ ^ ]
For further details, please see:
Environment.SpecialFolder Enum (System) | Microsoft Docs[^]
Environment.GetLogicalDrives Method (System) | Microsoft Docs[^]
How to Retrieve the REAL Hard Drive Serial Number[^]
这篇关于如何获取安装了Windows操作系统的硬盘序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!