Hi,
In MSDN document, you can find that Win32_LogicalDisk class is inherited
from CIM_LogicalDisk
class.
Because the ManagementObject you get from Win32_LogicalDisk not all have
the FreeSpace property
(Floppy disk and CDROM does not have this property value), you should check
every object's type before
retrieving the FreeSpace property.
Sample like this:
ManagementClass mc = new ManagementClass("Win32_LogicalDisk");
ManagementObjectCollection mco = mc.GetInstances();
foreach (ManagementObject mo in mco)
{
if(mo["DriveType"].ToString().Equals("3"))
{
MessageBox.Show( mo["FreeSpace"].ToString());
}
}
It works well on my machine, it will retrieve every hard disk's FreeSpace
on my machine.
Best regards,
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.
--------------------
| From: "New World Order Pigs" <
[email protected]>
| References: <#
[email protected]>
<#
[email protected]>
<#
[email protected]>
<
[email protected]>
<
[email protected]>
<
[email protected]>
| Subject: Re: disk space remaining?
| Date: Tue, 23 Sep 2003 11:11:32 -0600
| Lines: 198
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
| Message-ID: <
[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:186799
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I have discovered that "CIM_LogicalDisk" does work the way everyone else
| seems to use "WIN32_LogicalDrive"-- does anyone know why that would be?
Is
| this something I need to be concerned about as I deploy this code??? This
| all seems strange and I don't really trust it.
|
| NWOP
|
|
| | > Jeffrey, when I use your code verbatim I get the same thing. The "mo"
is
| > basically not initialized and there are error messages saying "error:
| cannot
| > obtain value," not sure what's going on here.
| >
| > NWOP
| >
| >
| > | > > Thanks Jeffrey. I can understand all of these points entirely. I
have
| > code
| > > that I've used that seems straight-forward and correct, but doesn't
| work--
| > > do you see anything wrong?
| > >
| > > ManagementClass mc = new ManagementClass("Win32_LogicalDisk");
| > > ManagementObjectCollection mco = mc.GetInstances();
| > > foreach (ManagementObject mo in mco)
| > > {
| > > i64 = mo["FreeSpace"];
| > > }
| > >
| > > This code snippet doesn't work and the "mo" objects don't seem to be
| > > initialized correctly.
| > >
| > > Thanks, NWOP.
| > >
| > > | > > >
| > > > Hi,
| > > >
| > > > DirectoryInfo does not have GetFreeSpace method, because it is
design
| > for
| > > > regular directory(
| > > > It makes no sense referring FreeSpace for regular directory).
| > > >
| > > > The System.Management class provided a way of retreiving the
| information
| > > > from WMI, the
| > > > WMI's function is monument and strong, so I think there is no need
for
| > the
| > > > Net to create a new class or
| > > > method for this.(If .Net really create a new class, it will interop
| with
| > > > WMI under hood)
| > > >
| > > > If you still do not want to use the System.Management class, you can
| > refer
| > > > to the FileSystemObject, like this:
| > > >
| > > > Add the "Microsoft Scripting RunTIme 1.0" from the "Add Reference"
| > > command,
| > > > "COM" tab page,
| > > >
| > > > 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+"'";
| > > > Scripting.Drive thisdrive=FSO.GetDrive(logDrives);
| > > > if (thisdrive.DriveType==Scripting.DriveTypeConst.Fixed )
| > > > {
| > > > MessageBox.Show(thisdrive.Path+"