取硬盘序列号?

Dugu_Niu 2004-10-09 10:47:26
使用WMI来取硬盘的序列号
using System.Management;
ManagementClass mc=new ManagementClass("Win32_PhysicalMedia");
foreach(ManagementObject mb in aa.GetInstances())
{
MessageBox.Show(bb.Properties["SerialNumber"].Value.ToString());
}

运行正常。
但在win98下,出现异常。

异常信息:
System.TypeInitializationException: “System.Management.ManagementBaseObject”的类型初始值设定项引发异常。 ---> System.Runtime.InteropServices.COMException (0x80040154): 带有 CLSID {674B6698-EE92-11D0-AD71-00C04FD8FDFF} 的 COM 对象无效或未注册。


那位大侠解释一下。还有什么办法可以取硬盘的物理序号?
...全文
1206 47 打赏 收藏 转发到动态 举报
写回复
用AI写文章
47 条回复
切换为时间正序
请发表友善的回复…
发表回复
oneby 2004-10-21
  • 打赏
  • 举报
回复
public string GetHd()
{
ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher();

wmiSearcher.Query = new SelectQuery(
"Win32_DiskDrive",
"",
new string[]{"PNPDeviceID"}
);
ManagementObjectCollection myCollection = wmiSearcher.Get();
ManagementObjectCollection.ManagementObjectEnumerator em =
myCollection.GetEnumerator();
string id="";
while(em.MoveNext())
{
ManagementBaseObject mo = em.Current;
try
{
id =id+"#"+ mo.Properties["PNPDeviceID"].Value.ToString().Trim();
}
catch(Exception ef)
{
id=ef.Message.ToString();
}
}
return id;
}

昨天看到的,在winfrom下可以得到硬盘序列号
在webfrom中报错:mo引用为空,为什么????????????????
CloneCenter 2004-10-21
  • 打赏
  • 举报
回复
强烈关注中……
hivak47 2004-10-16
  • 打赏
  • 举报
回复
using System;
using System.Runtime.InteropServices;

namespace ArLi.CommonPrj {

#region how use this?
/*
string sVol = getvol.GetVolOf("C");
*/
#endregion

public class getvol{

[DllImport("kernel32.dll")]
private static extern int GetVolumeInformation(
string lpRootPathName,
string lpVolumeNameBuffer,
int nVolumeNameSize,
ref int lpVolumeSerialNumber,
int lpMaximumComponentLength,
int lpFileSystemFlags,
string lpFileSystemNameBuffer,
int nFileSystemNameSize
);

public static string GetVolOf(string drvID){
const int MAX_FILENAME_LEN = 256;
int retVal = 0;
int a =0;
int b =0;
string str1 = null;
string str2 = null;


int i = GetVolumeInformation(
drvID + @":\",
str1,
MAX_FILENAME_LEN,
ref retVal,
a,
b,
str2,
MAX_FILENAME_LEN
);

return retVal.ToString("x");
}
}
}

yangzixp 2004-10-16
  • 打赏
  • 举报
回复
何不用C/++写一个控件?
速马 2004-10-12
  • 打赏
  • 举报
回复
可以按照lu0的思路封装个原生的dll,再interop,嗯哪
AhBian 2004-10-11
  • 打赏
  • 举报
回复
其实,C/C++ 也并非是小菜一碟。
AhBian 2004-10-11
  • 打赏
  • 举报
回复
楼主的异常是因为此方法只能用于 XP/2003 及以后的 Windows,这在 MSDN 上明确指出。

如果楼主要用 .NET 做共享/商业软件,是不能把 98 排斥在外的。
然而一个 .NET 软件要能在 98 上平滑的运行,是需要作者精心调整的。.NET 软件在 98 上真的很难侍候的。
我的经验是,判断一下操作系统版本,然后在某些容易引发(未预期的或者无法弥补的)异常的地方,选择不同的程序处理方式。

WMI 没有随 WIN98 一起安装,但可以另行安装,下载地址:
http://download.microsoft.com/download/platformsdk/wmi9x/1.5/W9X/EN-US/wmi9x.exe

对于获取硬盘序列号,C/C++ 是小菜一碟,而 .NET 真的很棘手。因为必须确保在不同的 OS 上获取的是完全一致的,(网上提供的)不同的方法应用在不同的 OS 上时,其结果未必相同。

我曾经发过一个类似的帖子,讨论这个问题,但最终还是觉得失败,因为不能达到广泛性和一致性这两个要求:
http://community.csdn.net/Expert/topic/2983/2983472.xml?temp=.2204248
Dugu_Niu 2004-10-10
  • 打赏
  • 举报
回复
to ifeasy(事在人为) :楼上有这个代码吗?
ifeasy 2004-10-10
  • 打赏
  • 举报
回复
我引用楼上的代码:
*********************************************************************
String HDid;
ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
HDid = (string)mo.Properties["Model"].Value;
MessageBox.Show(HDid );
}
********************************************************************
错误地方:ManagementObjectCollection moc = cimobject.GetInstances();
提示错误:异常详细信息: System.Management.ManagementException: 访问遭到拒绝
kong19 2004-10-10
  • 打赏
  • 举报
回复
[``]
Dugu_Niu 2004-10-10
  • 打赏
  • 举报
回复
to MiniNET(斗转星移) : 需要引用System.Management.dll
lglesias 2004-10-10
  • 打赏
  • 举报
回复
xuexi
up
wangdequan1024 2004-10-10
  • 打赏
  • 举报
回复
学习
MiniNET 2004-10-10
  • 打赏
  • 举报
回复
为什么 我的vs.net里没有 using System.Management;

我该怎么办,哪个朋友详细些说。谢谢 我的QQ:508905
速马 2004-10-10
  • 打赏
  • 举报
回复
The reason is that Win32_PhysicalMedia class is not suppored on Windows 98...
Dugu_Niu 2004-10-10
  • 打赏
  • 举报
回复
自己顶
阿_浩 2004-10-10
  • 打赏
  • 举报
回复
学习!
bflovesnow 2004-10-09
  • 打赏
  • 举报
回复
mark
lionqun 2004-10-09
  • 打赏
  • 举报
回复
Background
A better solution is to get the Hard Drive Serial Number given by the Manufacturer. This value won't change even if you format your Hard Drive. Of course if people buy new hard drive, we have a problem. But at least we keep the regular hard-drive formatters happy!

The code
First, let's create a class to store information about a hard drive

class HardDrive
{
private string model = null;
private string type = null;
private string serialNo = null;
public string Model
{
get {return model;}
set {model = value;}
}
public string Type
{
get {return type;}
set {type = value;}
}
public string SerialNo
{
get {return serialNo;}
set {serialNo = value;}
}
}

Next, add a reference to your project. Scroll down to "System.Management" under ComponentName. This reference is not provided by default, so you need to add it.

Add a "using System.Management;" at the top of your source code.

System.Management allows us to access WMI objects. Put simply, WMI contains a lot information about your hardware. Now there's a problem, the hard drive model is found in Win32_DiskDrive class, and the serial number is found in Win32_PhysicalMedia class, thus we need to query twice and integrate the information into our HardDrive class.

Let's first create an ArrayList to store our HardDrive objects
ArrayList hdCollection = new ArrayList();

Next, we query the Win32_DiskDrive class first:

ManagementObjectSearcher searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

foreach(ManagementObject wmi_HD in searcher.Get())
{
HardDrive hd = new HardDrive();
hd.Model = wmi_HD["Model"].ToString();
hd.Type = wmi_HD["InterfaceType"].ToString();
hdCollection.Add(hd);
}

Now we need to extract the serial number from Win32_PhysicalMedia class:


searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

int i = 0;
foreach(ManagementObject wmi_HD in searcher.Get())
{
// get the hard drive from collection
// using index
HardDrive hd = (HardDrive)hdCollection[i];

// get the hardware serial no.
if (wmi_HD["SerialNumber"] == null)
hd.SerialNo = "None";
else
hd.SerialNo = wmi_HD["SerialNumber"].ToString();

++i;
}
Luckily, the WMI objects from Win32_DiskDrive are in the same order as from Win32_PhysicalMedia, so the simple code above works. Otherwise, we will have to match them using their unique DeviceID. Start a thread in this article if you think Win32_DiskDrive objects are not the same order as Win32_PhysicalMedia objects returned.
We are done! Now we display our hard drives information:

// Display available hard drives
foreach(HardDrive hd in hdCollection)
{
Console.WriteLine("Model\t\t: " + hd.Model);
Console.WriteLine("Type\t\t: " + hd.Type);
Console.WriteLine("Serial No.\t: " + hd.SerialNo);
Console.WriteLine();
}

lengyue110 2004-10-09
  • 打赏
  • 举报
回复
学习
加载更多回复(27)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧