问题描述
我已经开发了一个使用硬盘序列号进行激活的应用程序但是当它来到使用多个硬盘的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;
}
推荐答案
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....
详情请见:
[]
[]
[]
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操作系统的硬盘序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!