Management Object vrs Scripting

  • Thread starter Thread starter andrewcw
  • Start date Start date
A

andrewcw

I have used System.Management in the past to extract and
walk thru drives & check their type. I can also do it
with COM's FileSystemObject. Here I am trying to just use
the Management Object after using the Directory class...

There seems to be a way to load the System Management
Object with string path ( 8 overloads ), but every time I
try a variation it crashes. Any ideas ? Thanks

Scripting.FileSystemObject FSO = new
Scripting.FileSystemObjectClass();
string [] logDrives=Directory.GetLogicalDrives();
for ( int i =0;i< logDrives.GetUpperBound(0)+1;i++)
{
System.Management.ManagementObject moDrive = new
System.Management.ManagementObject(logDrives); // crash
moDrive.Properties["DriveType"].Value.ToString();
Scripting.Drive thisdrive=FSO.GetDrive(logDrives);
if (thisdrive.DriveType==Scripting.DriveTypeConst.CDRom)
 
Hi Andrew,

You should pass the WMI path to the ManagementObject's constructor.
To get the disk information by ManagementObject, you should use parameter:
"Win32_LogicalDisk='[diskletter]:'"

So you can do something like this:

Scripting.FileSystemObject FSO = new Scripting.FileSystemObjectClass();
string [] logDrives=System.IO.Directory.GetLogicalDrives();
for ( int i =0;i< logDrives.GetUpperBound(0)+1;i++)
{
string disk=logDrives.Substring(0,2);
string param="Win32_LogicalDisk='"+disk+"'";
System.Management.ManagementObject moDrive = new
System.Management.ManagementObject(param); // crash
//moDrive.Properties["DriveType"].Value.ToString();
Scripting.Drive thisdrive=FSO.GetDrive(logDrives);
if (thisdrive.DriveType==Scripting.DriveTypeConst.CDRom )
{
MessageBox.Show(thisdrive.Path);
}
}

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "andrewcw" <[email protected]>
| Sender: "andrewcw" <[email protected]>
| Subject: Management Object vrs Scripting
| Date: Fri, 19 Sep 2003 14:24:16 -0700
| Lines: 19
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcN+9GEFc9zUkkAbSyuqmAfP8D/uLw==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:186216
| NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I have used System.Management in the past to extract and
| walk thru drives & check their type. I can also do it
| with COM's FileSystemObject. Here I am trying to just use
| the Management Object after using the Directory class...
|
| There seems to be a way to load the System Management
| Object with string path ( 8 overloads ), but every time I
| try a variation it crashes. Any ideas ? Thanks
|
| Scripting.FileSystemObject FSO = new
| Scripting.FileSystemObjectClass();
| string [] logDrives=Directory.GetLogicalDrives();
| for ( int i =0;i< logDrives.GetUpperBound(0)+1;i++)
| {
| System.Management.ManagementObject moDrive = new
| System.Management.ManagementObject(logDrives); // crash
| moDrive.Properties["DriveType"].Value.ToString();
| Scripting.Drive thisdrive=FSO.GetDrive(logDrives);
| if (thisdrive.DriveType==Scripting.DriveTypeConst.CDRom)
|
 
Back
Top