WMI for Win32_Volume not working

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.
 
A

Arne Janning

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
 
G

Guest

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.
 
J

Jeff Gaines

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.
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top