从MSDN文章中我发现-- http://msdn.microsoft.com/en-us/library/aa394515(v=VS.85).aspx -- Win32_Volume和Win32_MountPoint在Windows XP上不可用。
但是,我正在Windows XP (64位)上开发一个C#应用程序,我可以很好地访问这些WMI类。我的应用程序的用户将在装有.Net 3.5 sp1的Windows XP sp2上运行。
到处搜索,我不能确定我能不能依靠它。我能否在系统上成功运行是因为以下一个或多个原因:- .Net xp service pack 2?-安装了visual studio 2008 sp1?- windows 3.5 sp1?
我是否应该使用WMI以外的其他方法来获取卷/挂载点信息?
下面是正在运行的示例代码...
public static Dictionary<string, NameValueCollection> GetAllVolumeDeviceIDs()
{
Dictionary<string, NameValueCollection> ret = new Dictionary<string, NameValueCollection>();
// retrieve information from Win32_Volume
try
{
using (ManagementClass volClass = new ManagementClass("Win32_Volume"))
{
using (ManagementObjectCollection mocVols = volClass.GetInstances())
{
// iterate over every volume
foreach (ManagementObject moVol in mocVols)
{
// get the volume's device ID (will be key into our dictionary)
string devId = moVol.GetPropertyValue("DeviceID").ToString();
ret.Add(devId, new NameValueCollection());
//Console.WriteLine("Vol: {0}", devId);
// for each non-null property on the Volume, add it to our NameValueCollection
foreach (PropertyData p in moVol.Properties)
{
if (p.Value == null)
continue;
ret[devId].Add(p.Name, p.Value.ToString());
//Console.WriteLine("\t{0}: {1}", p.Name, p.Value);
}
// find the mountpoints of this volume
using (ManagementObjectCollection mocMPs = moVol.GetRelationships("Win32_MountPoint"))
{
foreach (ManagementObject moMP in mocMPs)
{
// only care about adding directory
// Directory prop will be something like "Win32_Directory.Name=\"C:\\\\\""
string dir = moMP["Directory"].ToString();
// find opening/closing quotes in order to get the substring we want
int first = dir.IndexOf('"') + 1;
int last = dir.LastIndexOf('"');
string dirSubstr = dir.Substring(first , last - first);
// use GetFullPath to normalize/unescape any extra backslashes
string fullpath = Path.GetFullPath(dirSubstr);
ret[devId].Add(MOUNTPOINT_DIRS_KEY, fullpath);
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Problem retrieving Volume information from WMI. {0} - \n{1}",ex.Message,ex.StackTrace);
return ret;
}
return ret;
}发布于 2010-05-12 15:35:48
我猜在Windows XP Professional x64版上可以使用Win32_MountPoint和Win32_Volume类,因为它是based on the Windows Server 2003 codebase。在32位版本的Windows XP上,这些类不存在,要执行任务,您需要像Tim所说的那样,P/调用本机卷管理函数。
发布于 2010-05-12 08:52:37
您可能需要将调用插入到Win32 Volume Management Functions中
https://stackoverflow.com/questions/2815296
复制相似问题