using System.Management to get volume info on a specific drive

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

Guest

Hi people,

I saw several code samples that first enumerate all drives using a query
with the ManagementObjectSearcher class:

<code>
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * From
Win32_LogicalDisk ");
ManagementObjectCollection queryCollection = query.Get();
</code>

How can I query for a specific drive (like D:)? Should I query it as
"SELECT * From Win32_LogicalDisk Where Name='D'"
???

Thanks & Kind regards,
 
Hey,

I found it, spells like:
SELECT * From Win32_LogicalDisk Where Name='d:'

Kind regards,
 
TT (Tom Tempelaere) said:
Hey,

I found it, spells like:
SELECT * From Win32_LogicalDisk Where Name='d:'

I still wonder how this could be done directly, not with a query. Since I
query for a specific drive there is only one entry and I have to fetch it
from the IEnumertion that encapsulates the results...

Kind regards,
 
TT said:
I still wonder how this could be done directly, not with a query.
Since I query for a specific drive there is only one entry and I have
to fetch it from the IEnumertion that encapsulates the results...

It is important that you understand what is happening when you use
System.Management. This namespace is a wrapper around the WMI API. This
is provided by a COM local server. So to make a query to
System.Management you have to make an inter process call via COM to the
WMI service.

I trust you are not too worried about performance. I certainly hope that
you do not make many calls to System.Management.

The simplest way to get information about the drives on your machine is
to use Win32 through platform invoke. These do not involve an inter
process call. .NET has Environment.GetLogicalDrives to get an array of
all drive types. You can then call various methods to get information:

GetDiskFreeSpaceEx
GetDriveType
GetVolumeInformation

etc

Richard
 
Hi Richard,

Richard Grimes said:
It is important that you understand what is happening when you use
System.Management. This namespace is a wrapper around the WMI API. This is
provided by a COM local server. So to make a query to System.Management
you have to make an inter process call via COM to the WMI service.

I trust you are not too worried about performance. I certainly hope that
you do not make many calls to System.Management.

The simplest way to get information about the drives on your machine is to
use Win32 through platform invoke. These do not involve an inter process
call. .NET has Environment.GetLogicalDrives to get an array of all drive
types. You can then call various methods to get information:

GetDiskFreeSpaceEx
GetDriveType
GetVolumeInformation

Richard

Actually I am concerned about performance, but I was trying to avoid
interop-ing with win32. The same is true for other things in my current
project, if it is possible to avoid interop I usually do. Currently I'm only
interoping for file transfer into unmanaged memory blocks, and third party
C-libraries.

I noticed that the call is indeed slow, but I think that is due to the
implementation of WMI and not the fact its interface is exposed through COM
(correct me if I'm wrong). The call will indeed have to be done regularly,
but not constantly.

How long would it take to make the query I posted (on an average PC)? Order
of milliseconds? More?

Kind regards,
 
Back
Top