WMI for Win32_Volume not working

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to execute this simple query:

ObjectQuery objectQuery = new ObjectQuery("Select * FROM Win32_Volume");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(objectQuery);
ManagementObjectCollection moc = searcher.Get();
try
{
foreach (ManagementObject volume in moc)
{
Console.WriteLine("Label = " + volume["Label"]);
}
}
catch (ManagementException e)
{
Console.WriteLine(e.Message);
}

But I get an ManagementException thrown in the foreach (not on the
Console.WriteLine that follows)

The message is simply "Invalid Class" which doesn't really tell me a whole
lot.

Looking around the web, I've found a couple of pieces of code that aren't
too different from this, but nothing to explain why I would be getting this
problem.

I'm running VS.NET 2003 on Win2K Professional.

Thanks for any help anyone can give.
 
I'm trying to execute this simple query:

ObjectQuery objectQuery = new ObjectQuery("Select * FROM Win32_Volume");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(objectQuery);
ManagementObjectCollection moc = searcher.Get();
try
{
foreach (ManagementObject volume in moc)
{
Console.WriteLine("Label = " + volume["Label"]);
}
}
catch (ManagementException e)
{
Console.WriteLine(e.Message);
}

But I get an ManagementException thrown in the foreach (not on the
Console.WriteLine that follows)

The message is simply "Invalid Class" which doesn't really tell me a whole
lot.

Looking around the web, I've found a couple of pieces of code that aren't
too different from this, but nothing to explain why I would be getting this
problem.

I'm running VS.NET 2003 on Win2K Professional.

Hi (e-mail address removed),

your code is compiling and running without problems on my machine.

Are you sure you added the reference to the System.Management.dll and
that you added

using System.Management;

to your code?

Cheers

Arne Janning
 
I had a feeling that might be the case (that's why I mentioned my OS),
though I thought it was going to be an XP-only thing.

Really, I only need one thing, the volume label. How can I get it without
the Win32_Volume WMI?

Pete

Ken Tucker said:
Hi,

Win32_Volume isn't availble on windows xp or earlier.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_volume.asp

Ken
-----------------
I'm trying to execute this simple query:

ObjectQuery objectQuery = new ObjectQuery("Select * FROM Win32_Volume");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(objectQuery);
ManagementObjectCollection moc = searcher.Get();
try
{
foreach (ManagementObject volume in moc)
{
Console.WriteLine("Label = " + volume["Label"]);
}
}
catch (ManagementException e)
{
Console.WriteLine(e.Message);
}

But I get an ManagementException thrown in the foreach (not on the
Console.WriteLine that follows)

The message is simply "Invalid Class" which doesn't really tell me a whole
lot.

Looking around the web, I've found a couple of pieces of code that aren't
too different from this, but nothing to explain why I would be getting
this
problem.

I'm running VS.NET 2003 on Win2K Professional.

Thanks for any help anyone can give.
 
I had a feeling that might be the case (that's why I mentioned my OS),
though I thought it was going to be an XP-only thing.

Really, I only need one thing, the volume label. How can I get it without
the Win32_Volume WMI?

Pete


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_volume.asp

[snipped]

How about:
// GetVolumeInformation
[DllImport("kernel32.dll",
EntryPoint="GetVolumeInformation", SetLastError=true,
CallingConvention=CallingConvention.StdCall)]
public static extern int GetVolumeInformation(
[MarshalAs(UnmanagedType.LPStr)]
string lpRootPathName,
StringBuilder lpVolumeNameBuffer,
int nVolumeNameSize,
ref int lpVolumeSerialNumber,
ref int lpMaximumComponentLength,
ref int lpFileSystemFlags,
StringBuilder lpFileSystemNameBuffer,
int nFileSystemNameSize);

It gets a lot of other stuff as well but it's quick.
 
Yeah, that's the way I went. I was hoping there was a managed method. I
really hate going outside of the managed API if possible, but I guess I
don't have a choice in this case.

Thanks.

Pete

Jeff Gaines said:
I had a feeling that might be the case (that's why I mentioned my OS),
though I thought it was going to be an XP-only thing.

Really, I only need one thing, the volume label. How can I get it without
the Win32_Volume WMI?

Pete


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi
/win32_volume.asp

[snipped]

How about:
// GetVolumeInformation
[DllImport("kernel32.dll",
EntryPoint="GetVolumeInformation", SetLastError=true,
CallingConvention=CallingConvention.StdCall)]
public static extern int GetVolumeInformation(
[MarshalAs(UnmanagedType.LPStr)]
string lpRootPathName,
StringBuilder lpVolumeNameBuffer,
int nVolumeNameSize,
ref int lpVolumeSerialNumber,
ref int lpMaximumComponentLength,
ref int lpFileSystemFlags,
StringBuilder lpFileSystemNameBuffer,
int nFileSystemNameSize);

It gets a lot of other stuff as well but it's quick.
 
Back
Top