By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

disk space remaining?

P: n/a
Is there no way in .net to get disk space remaining for a given drive??? I
can't believe it and yet is seems to be so. If someone knows of a way to do
this in the .net libraries I'd be very much appreciative.

Thanks, LT.
Nov 15 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a

"New World Order Pigs" <lt*******@md-it.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Is there no way in .net to get disk space remaining for a given drive??? I
can't believe it and yet is seems to be so. If someone knows of a way to do
this in the .net libraries I'd be very much appreciative.

Thanks, LT.


You could use the Management namespace classes (and WMI).
Here's a small sample:

using System;
using System.Management;
class Tester {
public static void Main() {
Int64 i = GetFreeSpace("C:");
Console.WriteLine(i);
}

public static Int64 GetFreeSpace(string logicalDrive )
{
Int64 nRet = 0;
// Create a query
String strSQL = "SELECT FreeSpace FROM Win32_LogicalDisk WHERE DeviceID='" + logicalDrive + "'" ;
try
{
SelectQuery query = new SelectQuery(strSQL);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);

foreach(ManagementBaseObject drive in searcher.Get())
{
UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace property
nRet = (Int64)u;
}

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return nRet/1024; // return KB
} // GetFreeSpace

}

Willy.
Nov 15 '05 #2

P: n/a
Thanks Willy. I tried something similar to this and it didn't seem to work.
I was hoping there was something in the .net libraries that allowed a direct
function call and not the System.Management class. I'm still in a state of
shock regarding this and just can't believe you can't just do something like
"System.IO.DirectoryInfo.GetFreeSpace" or something similar...

Thanks Willy. Apparently I have to keep playing with the System Management
class though.
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:#r*************@TK2MSFTNGP09.phx.gbl...

"New World Order Pigs" <lt*******@md-it.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Is there no way in .net to get disk space remaining for a given drive??? I can't believe it and yet is seems to be so. If someone knows of a way to do this in the .net libraries I'd be very much appreciative.

Thanks, LT.


You could use the Management namespace classes (and WMI).
Here's a small sample:

using System;
using System.Management;
class Tester {
public static void Main() {
Int64 i = GetFreeSpace("C:");
Console.WriteLine(i);
}

public static Int64 GetFreeSpace(string logicalDrive )
{
Int64 nRet = 0;
// Create a query
String strSQL = "SELECT FreeSpace FROM Win32_LogicalDisk WHERE

DeviceID='" + logicalDrive + "'" ; try
{
SelectQuery query = new SelectQuery(strSQL);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach(ManagementBaseObject drive in searcher.Get())
{
UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace property
nRet = (Int64)u;
}

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return nRet/1024; // return KB
} // GetFreeSpace

}

Willy.

Nov 15 '05 #3

P: n/a

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[i].Substring(0,2);
string param="Win32_LogicalDisk='"+disk+"'";
Scripting.Drive thisdrive=FSO.GetDrive(logDrives[i]);
if (thisdrive.DriveType==Scripting.DriveTypeConst.Fix ed )
{
MessageBox.Show(thisdrive.Path+" FreeSpace:"+thisdrive.FreeSpace );
}
}

Hope this helps,
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" <lt*******@md-it.com>
| References: <#B**************@TK2MSFTNGP09.phx.gbl>
<#r*************@TK2MSFTNGP09.phx.gbl>
| Subject: Re: disk space remaining?
| Date: Mon, 22 Sep 2003 17:02:35 -0600
| Lines: 69
| 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: <#j*************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:186661
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks Willy. I tried something similar to this and it didn't seem to
work.
| I was hoping there was something in the .net libraries that allowed a
direct
| function call and not the System.Management class. I'm still in a state
of
| shock regarding this and just can't believe you can't just do something
like
| "System.IO.DirectoryInfo.GetFreeSpace" or something similar...
|
| Thanks Willy. Apparently I have to keep playing with the System
Management
| class though.
|
|
| "Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
| news:#r*************@TK2MSFTNGP09.phx.gbl...
| >
| > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| news:%2****************@TK2MSFTNGP09.phx.gbl...
| > > Is there no way in .net to get disk space remaining for a given
drive???
| I
| > > can't believe it and yet is seems to be so. If someone knows of a way
| to do
| > > this in the .net libraries I'd be very much appreciative.
| > >
| > > Thanks, LT.
| > >
| > >
| >
| > You could use the Management namespace classes (and WMI).
| > Here's a small sample:
| >
| > using System;
| > using System.Management;
| > class Tester {
| > public static void Main() {
| > Int64 i = GetFreeSpace("C:");
| > Console.WriteLine(i);
| > }
| >
| > public static Int64 GetFreeSpace(string logicalDrive )
| > {
| > Int64 nRet = 0;
| > // Create a query
| > String strSQL = "SELECT FreeSpace FROM Win32_LogicalDisk WHERE
| DeviceID='" + logicalDrive + "'" ;
| > try
| > {
| > SelectQuery query = new SelectQuery(strSQL);
| > ManagementObjectSearcher searcher = new
| ManagementObjectSearcher(query);
| >
| > foreach(ManagementBaseObject drive in searcher.Get())
| > {
| > UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace property
| > nRet = (Int64)u;
| > }
| >
| > }
| > catch (Exception e)
| > {
| > Console.WriteLine(e.ToString());
| > }
| > return nRet/1024; // return KB
| > } // GetFreeSpace
| >
| > }
| >
| > Willy.
| >
| >
|
|
|

Nov 15 '05 #4

P: n/a
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.

"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
news:Jm**************@cpmsftngxa06.phx.gbl...

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[i].Substring(0,2);
string param="Win32_LogicalDisk='"+disk+"'";
Scripting.Drive thisdrive=FSO.GetDrive(logDrives[i]);
if (thisdrive.DriveType==Scripting.DriveTypeConst.Fix ed )
{
MessageBox.Show(thisdrive.Path+" FreeSpace:"+thisdrive.FreeSpace );
}
}

Hope this helps,
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" <lt*******@md-it.com>
| References: <#B**************@TK2MSFTNGP09.phx.gbl>
<#r*************@TK2MSFTNGP09.phx.gbl>
| Subject: Re: disk space remaining?
| Date: Mon, 22 Sep 2003 17:02:35 -0600
| Lines: 69
| 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: <#j*************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:186661 | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks Willy. I tried something similar to this and it didn't seem to
work.
| I was hoping there was something in the .net libraries that allowed a
direct
| function call and not the System.Management class. I'm still in a state
of
| shock regarding this and just can't believe you can't just do something
like
| "System.IO.DirectoryInfo.GetFreeSpace" or something similar...
|
| Thanks Willy. Apparently I have to keep playing with the System
Management
| class though.
|
|
| "Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
| news:#r*************@TK2MSFTNGP09.phx.gbl...
| >
| > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| news:%2****************@TK2MSFTNGP09.phx.gbl...
| > > Is there no way in .net to get disk space remaining for a given
drive???
| I
| > > can't believe it and yet is seems to be so. If someone knows of a way | to do
| > > this in the .net libraries I'd be very much appreciative.
| > >
| > > Thanks, LT.
| > >
| > >
| >
| > You could use the Management namespace classes (and WMI).
| > Here's a small sample:
| >
| > using System;
| > using System.Management;
| > class Tester {
| > public static void Main() {
| > Int64 i = GetFreeSpace("C:");
| > Console.WriteLine(i);
| > }
| >
| > public static Int64 GetFreeSpace(string logicalDrive )
| > {
| > Int64 nRet = 0;
| > // Create a query
| > String strSQL = "SELECT FreeSpace FROM Win32_LogicalDisk WHERE
| DeviceID='" + logicalDrive + "'" ;
| > try
| > {
| > SelectQuery query = new SelectQuery(strSQL);
| > ManagementObjectSearcher searcher = new
| ManagementObjectSearcher(query);
| >
| > foreach(ManagementBaseObject drive in searcher.Get())
| > {
| > UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace property | > nRet = (Int64)u;
| > }
| >
| > }
| > catch (Exception e)
| > {
| > Console.WriteLine(e.ToString());
| > }
| > return nRet/1024; // return KB
| > } // GetFreeSpace
| >
| > }
| >
| > Willy.
| >
| >
|
|
|

Nov 15 '05 #5

P: n/a
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
"New World Order Pigs" <lt*******@md-it.com> wrote in message
news:eQ*************@TK2MSFTNGP10.phx.gbl...
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.

"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
news:Jm**************@cpmsftngxa06.phx.gbl...

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[i].Substring(0,2);
string param="Win32_LogicalDisk='"+disk+"'";
Scripting.Drive thisdrive=FSO.GetDrive(logDrives[i]);
if (thisdrive.DriveType==Scripting.DriveTypeConst.Fix ed )
{
MessageBox.Show(thisdrive.Path+" FreeSpace:"+thisdrive.FreeSpace );
}
}

Hope this helps,
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" <lt*******@md-it.com>
| References: <#B**************@TK2MSFTNGP09.phx.gbl>
<#r*************@TK2MSFTNGP09.phx.gbl>
| Subject: Re: disk space remaining?
| Date: Mon, 22 Sep 2003 17:02:35 -0600
| Lines: 69
| 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: <#j*************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl

microsoft.public.dotnet.languages.csharp:186661
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks Willy. I tried something similar to this and it didn't seem to
work.
| I was hoping there was something in the .net libraries that allowed a
direct
| function call and not the System.Management class. I'm still in a state of
| shock regarding this and just can't believe you can't just do something like
| "System.IO.DirectoryInfo.GetFreeSpace" or something similar...
|
| Thanks Willy. Apparently I have to keep playing with the System
Management
| class though.
|
|
| "Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
| news:#r*************@TK2MSFTNGP09.phx.gbl...
| >
| > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| news:%2****************@TK2MSFTNGP09.phx.gbl...
| > > Is there no way in .net to get disk space remaining for a given
drive???
| I
| > > can't believe it and yet is seems to be so. If someone knows of a

way
| to do
| > > this in the .net libraries I'd be very much appreciative.
| > >
| > > Thanks, LT.
| > >
| > >
| >
| > You could use the Management namespace classes (and WMI).
| > Here's a small sample:
| >
| > using System;
| > using System.Management;
| > class Tester {
| > public static void Main() {
| > Int64 i = GetFreeSpace("C:");
| > Console.WriteLine(i);
| > }
| >
| > public static Int64 GetFreeSpace(string logicalDrive )
| > {
| > Int64 nRet = 0;
| > // Create a query
| > String strSQL = "SELECT FreeSpace FROM Win32_LogicalDisk WHERE
| DeviceID='" + logicalDrive + "'" ;
| > try
| > {
| > SelectQuery query = new SelectQuery(strSQL);
| > ManagementObjectSearcher searcher = new
| ManagementObjectSearcher(query);
| >
| > foreach(ManagementBaseObject drive in searcher.Get())
| > {
| > UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace

property
| > nRet = (Int64)u;
| > }
| >
| > }
| > catch (Exception e)
| > {
| > Console.WriteLine(e.ToString());
| > }
| > return nRet/1024; // return KB
| > } // GetFreeSpace
| >
| > }
| >
| > Willy.
| >
| >
|
|
|


Nov 15 '05 #6

P: n/a
OK, if I use "CIM_LogicalDisk" then everything works... Why is that? Does
anyone know and as I deploy thi
"New World Order Pigs" <lt*******@md-it.com> wrote in message
news:u3**************@TK2MSFTNGP09.phx.gbl...
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
"New World Order Pigs" <lt*******@md-it.com> wrote in message
news:eQ*************@TK2MSFTNGP10.phx.gbl...
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.

"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
news:Jm**************@cpmsftngxa06.phx.gbl...

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[i].Substring(0,2);
string param="Win32_LogicalDisk='"+disk+"'";
Scripting.Drive thisdrive=FSO.GetDrive(logDrives[i]);
if (thisdrive.DriveType==Scripting.DriveTypeConst.Fix ed )
{
MessageBox.Show(thisdrive.Path+" FreeSpace:"+thisdrive.FreeSpace );
}
}

Hope this helps,
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" <lt*******@md-it.com>
| References: <#B**************@TK2MSFTNGP09.phx.gbl>
<#r*************@TK2MSFTNGP09.phx.gbl>
| Subject: Re: disk space remaining?
| Date: Mon, 22 Sep 2003 17:02:35 -0600
| Lines: 69
| 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: <#j*************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl

microsoft.public.dotnet.languages.csharp:186661
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks Willy. I tried something similar to this and it didn't seem to work.
| I was hoping there was something in the .net libraries that allowed a direct
| function call and not the System.Management class. I'm still in a state of
| shock regarding this and just can't believe you can't just do something like
| "System.IO.DirectoryInfo.GetFreeSpace" or something similar...
|
| Thanks Willy. Apparently I have to keep playing with the System
Management
| class though.
|
|
| "Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message | news:#r*************@TK2MSFTNGP09.phx.gbl...
| >
| > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| news:%2****************@TK2MSFTNGP09.phx.gbl...
| > > Is there no way in .net to get disk space remaining for a given
drive???
| I
| > > can't believe it and yet is seems to be so. If someone knows of

a way
| to do
| > > this in the .net libraries I'd be very much appreciative.
| > >
| > > Thanks, LT.
| > >
| > >
| >
| > You could use the Management namespace classes (and WMI).
| > Here's a small sample:
| >
| > using System;
| > using System.Management;
| > class Tester {
| > public static void Main() {
| > Int64 i = GetFreeSpace("C:");
| > Console.WriteLine(i);
| > }
| >
| > public static Int64 GetFreeSpace(string logicalDrive )
| > {
| > Int64 nRet = 0;
| > // Create a query
| > String strSQL = "SELECT FreeSpace FROM Win32_LogicalDisk WHERE
| DeviceID='" + logicalDrive + "'" ;
| > try
| > {
| > SelectQuery query = new SelectQuery(strSQL);
| > ManagementObjectSearcher searcher = new
| ManagementObjectSearcher(query);
| >
| > foreach(ManagementBaseObject drive in searcher.Get())
| > {
| > UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace

property
| > nRet = (Int64)u;
| > }
| >
| > }
| > catch (Exception e)
| > {
| > Console.WriteLine(e.ToString());
| > }
| > return nRet/1024; // return KB
| > } // GetFreeSpace
| >
| > }
| >
| > Willy.
| >
| >
|
|
|



Nov 15 '05 #7

P: n/a
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
"New World Order Pigs" <lt*******@md-it.com> wrote in message
news:u3**************@TK2MSFTNGP09.phx.gbl...
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
"New World Order Pigs" <lt*******@md-it.com> wrote in message
news:eQ*************@TK2MSFTNGP10.phx.gbl...
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.

"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
news:Jm**************@cpmsftngxa06.phx.gbl...

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[i].Substring(0,2);
string param="Win32_LogicalDisk='"+disk+"'";
Scripting.Drive thisdrive=FSO.GetDrive(logDrives[i]);
if (thisdrive.DriveType==Scripting.DriveTypeConst.Fix ed )
{
MessageBox.Show(thisdrive.Path+" FreeSpace:"+thisdrive.FreeSpace );
}
}

Hope this helps,
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" <lt*******@md-it.com>
| References: <#B**************@TK2MSFTNGP09.phx.gbl>
<#r*************@TK2MSFTNGP09.phx.gbl>
| Subject: Re: disk space remaining?
| Date: Mon, 22 Sep 2003 17:02:35 -0600
| Lines: 69
| 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: <#j*************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl

microsoft.public.dotnet.languages.csharp:186661
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks Willy. I tried something similar to this and it didn't seem to work.
| I was hoping there was something in the .net libraries that allowed a direct
| function call and not the System.Management class. I'm still in a state of
| shock regarding this and just can't believe you can't just do something like
| "System.IO.DirectoryInfo.GetFreeSpace" or something similar...
|
| Thanks Willy. Apparently I have to keep playing with the System
Management
| class though.
|
|
| "Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message | news:#r*************@TK2MSFTNGP09.phx.gbl...
| >
| > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| news:%2****************@TK2MSFTNGP09.phx.gbl...
| > > Is there no way in .net to get disk space remaining for a given
drive???
| I
| > > can't believe it and yet is seems to be so. If someone knows of

a way
| to do
| > > this in the .net libraries I'd be very much appreciative.
| > >
| > > Thanks, LT.
| > >
| > >
| >
| > You could use the Management namespace classes (and WMI).
| > Here's a small sample:
| >
| > using System;
| > using System.Management;
| > class Tester {
| > public static void Main() {
| > Int64 i = GetFreeSpace("C:");
| > Console.WriteLine(i);
| > }
| >
| > public static Int64 GetFreeSpace(string logicalDrive )
| > {
| > Int64 nRet = 0;
| > // Create a query
| > String strSQL = "SELECT FreeSpace FROM Win32_LogicalDisk WHERE
| DeviceID='" + logicalDrive + "'" ;
| > try
| > {
| > SelectQuery query = new SelectQuery(strSQL);
| > ManagementObjectSearcher searcher = new
| ManagementObjectSearcher(query);
| >
| > foreach(ManagementBaseObject drive in searcher.Get())
| > {
| > UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace

property
| > nRet = (Int64)u;
| > }
| >
| > }
| > catch (Exception e)
| > {
| > Console.WriteLine(e.ToString());
| > }
| > return nRet/1024; // return KB
| > } // GetFreeSpace
| >
| > }
| >
| > Willy.
| >
| >
|
|
|



Nov 15 '05 #8

P: n/a

"New World Order Pigs" <lt*******@md-it.com> wrote in message news:Oq**************@TK2MSFTNGP10.phx.gbl...
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


What OS are you running?

Willy.
Nov 15 '05 #9

P: n/a
Win2000 pro and it also did work on Win2000 server. I haven't tried it on
XP yet. Again, this is using the "CIM_LogicalDisk" instead of
"Win32_LogicalDisk." I just wonder if this is a potential problem. The
code involved in this endeavor is critical.

Thanks, NWOP.

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:ef**************@TK2MSFTNGP11.phx.gbl...

"New World Order Pigs" <lt*******@md-it.com> wrote in message

news:Oq**************@TK2MSFTNGP10.phx.gbl...
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


What OS are you running?

Willy.

Nov 15 '05 #10

P: n/a

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" <lt*******@md-it.com>
| References: <#B**************@TK2MSFTNGP09.phx.gbl>
<#r*************@TK2MSFTNGP09.phx.gbl>
<#j*************@TK2MSFTNGP11.phx.gbl>
<Jm**************@cpmsftngxa06.phx.gbl>
<eQ*************@TK2MSFTNGP10.phx.gbl>
<u3**************@TK2MSFTNGP09.phx.gbl>
| 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: <Oq**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.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
|
|
| "New World Order Pigs" <lt*******@md-it.com> wrote in message
| news:u3**************@TK2MSFTNGP09.phx.gbl...
| > 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
| >
| >
| > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| > news:eQ*************@TK2MSFTNGP10.phx.gbl...
| > > 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.
| > >
| > > "Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
| > > news:Jm**************@cpmsftngxa06.phx.gbl...
| > > >
| > > > 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[i].Substring(0,2);
| > > > string param="Win32_LogicalDisk='"+disk+"'";
| > > > Scripting.Drive thisdrive=FSO.GetDrive(logDrives[i]);
| > > > if (thisdrive.DriveType==Scripting.DriveTypeConst.Fix ed )
| > > > {
| > > > MessageBox.Show(thisdrive.Path+" FreeSpace:"+thisdrive.FreeSpace );
| > > > }
| > > > }
| > > >
| > > > Hope this helps,
| > > > 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" <lt*******@md-it.com>
| > > > | References: <#B**************@TK2MSFTNGP09.phx.gbl>
| > > > <#r*************@TK2MSFTNGP09.phx.gbl>
| > > > | Subject: Re: disk space remaining?
| > > > | Date: Mon, 22 Sep 2003 17:02:35 -0600
| > > > | Lines: 69
| > > > | 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: <#j*************@TK2MSFTNGP11.phx.gbl>
| > > > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > > > | NNTP-Posting-Host: 64.207.45.130
| > > > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| > > > | Xref: cpmsftngxa06.phx.gbl
| > > microsoft.public.dotnet.languages.csharp:186661
| > > > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > > > |
| > > > | Thanks Willy. I tried something similar to this and it didn't
seem
| to
| > > > work.
| > > > | I was hoping there was something in the .net libraries that
allowed
| a
| > > > direct
| > > > | function call and not the System.Management class. I'm still in a
| > state
| > > > of
| > > > | shock regarding this and just can't believe you can't just do
| > something
| > > > like
| > > > | "System.IO.DirectoryInfo.GetFreeSpace" or something similar...
| > > > |
| > > > | Thanks Willy. Apparently I have to keep playing with the System
| > > > Management
| > > > | class though.
| > > > |
| > > > |
| > > > | "Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in
| message
| > > > | news:#r*************@TK2MSFTNGP09.phx.gbl...
| > > > | >
| > > > | > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| > > > | news:%2****************@TK2MSFTNGP09.phx.gbl...
| > > > | > > Is there no way in .net to get disk space remaining for a
given
| > > > drive???
| > > > | I
| > > > | > > can't believe it and yet is seems to be so. If someone knows
of
| a
| > > way
| > > > | to do
| > > > | > > this in the .net libraries I'd be very much appreciative.
| > > > | > >
| > > > | > > Thanks, LT.
| > > > | > >
| > > > | > >
| > > > | >
| > > > | > You could use the Management namespace classes (and WMI).
| > > > | > Here's a small sample:
| > > > | >
| > > > | > using System;
| > > > | > using System.Management;
| > > > | > class Tester {
| > > > | > public static void Main() {
| > > > | > Int64 i = GetFreeSpace("C:");
| > > > | > Console.WriteLine(i);
| > > > | > }
| > > > | >
| > > > | > public static Int64 GetFreeSpace(string logicalDrive )
| > > > | > {
| > > > | > Int64 nRet = 0;
| > > > | > // Create a query
| > > > | > String strSQL = "SELECT FreeSpace FROM Win32_LogicalDisk
WHERE
| > > > | DeviceID='" + logicalDrive + "'" ;
| > > > | > try
| > > > | > {
| > > > | > SelectQuery query = new SelectQuery(strSQL);
| > > > | > ManagementObjectSearcher searcher = new
| > > > | ManagementObjectSearcher(query);
| > > > | >
| > > > | > foreach(ManagementBaseObject drive in searcher.Get())
| > > > | > {
| > > > | > UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace
| > > property
| > > > | > nRet = (Int64)u;
| > > > | > }
| > > > | >
| > > > | > }
| > > > | > catch (Exception e)
| > > > | > {
| > > > | > Console.WriteLine(e.ToString());
| > > > | > }
| > > > | > return nRet/1024; // return KB
| > > > | > } // GetFreeSpace
| > > > | >
| > > > | > }
| > > > | >
| > > > | > Willy.
| > > > | >
| > > > | >
| > > > |
| > > > |
| > > > |
| > > >
| > >
| > >
| >
| >
|
|
|

Nov 15 '05 #11

P: n/a
Hey, that's it and that is awesome-- thanks so much Jeffrey.

NWOP

"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
news:Me**************@cpmsftngxa06.phx.gbl...

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" <lt*******@md-it.com>
| References: <#B**************@TK2MSFTNGP09.phx.gbl>
<#r*************@TK2MSFTNGP09.phx.gbl>
<#j*************@TK2MSFTNGP11.phx.gbl>
<Jm**************@cpmsftngxa06.phx.gbl>
<eQ*************@TK2MSFTNGP10.phx.gbl>
<u3**************@TK2MSFTNGP09.phx.gbl>
| 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: <Oq**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.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
|
|
| "New World Order Pigs" <lt*******@md-it.com> wrote in message
| news:u3**************@TK2MSFTNGP09.phx.gbl...
| > 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
| >
| >
| > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| > news:eQ*************@TK2MSFTNGP10.phx.gbl...
| > > 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.
| > >
| > > "Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
| > > news:Jm**************@cpmsftngxa06.phx.gbl...
| > > >
| > > > 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[i].Substring(0,2);
| > > > string param="Win32_LogicalDisk='"+disk+"'";
| > > > Scripting.Drive thisdrive=FSO.GetDrive(logDrives[i]);
| > > > if (thisdrive.DriveType==Scripting.DriveTypeConst.Fix ed )
| > > > {
| > > > MessageBox.Show(thisdrive.Path+" FreeSpace:"+thisdrive.FreeSpace ); | > > > }
| > > > }
| > > >
| > > > Hope this helps,
| > > > 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" <lt*******@md-it.com>
| > > > | References: <#B**************@TK2MSFTNGP09.phx.gbl>
| > > > <#r*************@TK2MSFTNGP09.phx.gbl>
| > > > | Subject: Re: disk space remaining?
| > > > | Date: Mon, 22 Sep 2003 17:02:35 -0600
| > > > | Lines: 69
| > > > | 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: <#j*************@TK2MSFTNGP11.phx.gbl>
| > > > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > > > | NNTP-Posting-Host: 64.207.45.130
| > > > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| > > > | Xref: cpmsftngxa06.phx.gbl
| > > microsoft.public.dotnet.languages.csharp:186661
| > > > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > > > |
| > > > | Thanks Willy. I tried something similar to this and it didn't
seem
| to
| > > > work.
| > > > | I was hoping there was something in the .net libraries that
allowed
| a
| > > > direct
| > > > | function call and not the System.Management class. I'm still in a | > state
| > > > of
| > > > | shock regarding this and just can't believe you can't just do
| > something
| > > > like
| > > > | "System.IO.DirectoryInfo.GetFreeSpace" or something similar...
| > > > |
| > > > | Thanks Willy. Apparently I have to keep playing with the System | > > > Management
| > > > | class though.
| > > > |
| > > > |
| > > > | "Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in
| message
| > > > | news:#r*************@TK2MSFTNGP09.phx.gbl...
| > > > | >
| > > > | > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| > > > | news:%2****************@TK2MSFTNGP09.phx.gbl...
| > > > | > > Is there no way in .net to get disk space remaining for a
given
| > > > drive???
| > > > | I
| > > > | > > can't believe it and yet is seems to be so. If someone knows of
| a
| > > way
| > > > | to do
| > > > | > > this in the .net libraries I'd be very much appreciative.
| > > > | > >
| > > > | > > Thanks, LT.
| > > > | > >
| > > > | > >
| > > > | >
| > > > | > You could use the Management namespace classes (and WMI).
| > > > | > Here's a small sample:
| > > > | >
| > > > | > using System;
| > > > | > using System.Management;
| > > > | > class Tester {
| > > > | > public static void Main() {
| > > > | > Int64 i = GetFreeSpace("C:");
| > > > | > Console.WriteLine(i);
| > > > | > }
| > > > | >
| > > > | > public static Int64 GetFreeSpace(string logicalDrive )
| > > > | > {
| > > > | > Int64 nRet = 0;
| > > > | > // Create a query
| > > > | > String strSQL = "SELECT FreeSpace FROM Win32_LogicalDisk
WHERE
| > > > | DeviceID='" + logicalDrive + "'" ;
| > > > | > try
| > > > | > {
| > > > | > SelectQuery query = new SelectQuery(strSQL);
| > > > | > ManagementObjectSearcher searcher = new
| > > > | ManagementObjectSearcher(query);
| > > > | >
| > > > | > foreach(ManagementBaseObject drive in searcher.Get())
| > > > | > {
| > > > | > UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace | > > property
| > > > | > nRet = (Int64)u;
| > > > | > }
| > > > | >
| > > > | > }
| > > > | > catch (Exception e)
| > > > | > {
| > > > | > Console.WriteLine(e.ToString());
| > > > | > }
| > > > | > return nRet/1024; // return KB
| > > > | > } // GetFreeSpace
| > > > | >
| > > > | > }
| > > > | >
| > > > | > Willy.
| > > > | >
| > > > | >
| > > > |
| > > > |
| > > > |
| > > >
| > >
| > >
| >
| >
|
|
|

Nov 15 '05 #12

P: n/a
Hey, that's it and that is awesome-- thanks so much Jeffrey.

NWOP

"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
news:Me**************@cpmsftngxa06.phx.gbl...

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" <lt*******@md-it.com>
| References: <#B**************@TK2MSFTNGP09.phx.gbl>
<#r*************@TK2MSFTNGP09.phx.gbl>
<#j*************@TK2MSFTNGP11.phx.gbl>
<Jm**************@cpmsftngxa06.phx.gbl>
<eQ*************@TK2MSFTNGP10.phx.gbl>
<u3**************@TK2MSFTNGP09.phx.gbl>
| 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: <Oq**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.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
|
|
| "New World Order Pigs" <lt*******@md-it.com> wrote in message
| news:u3**************@TK2MSFTNGP09.phx.gbl...
| > 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
| >
| >
| > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| > news:eQ*************@TK2MSFTNGP10.phx.gbl...
| > > 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.
| > >
| > > "Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
| > > news:Jm**************@cpmsftngxa06.phx.gbl...
| > > >
| > > > 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[i].Substring(0,2);
| > > > string param="Win32_LogicalDisk='"+disk+"'";
| > > > Scripting.Drive thisdrive=FSO.GetDrive(logDrives[i]);
| > > > if (thisdrive.DriveType==Scripting.DriveTypeConst.Fix ed )
| > > > {
| > > > MessageBox.Show(thisdrive.Path+" FreeSpace:"+thisdrive.FreeSpace ); | > > > }
| > > > }
| > > >
| > > > Hope this helps,
| > > > 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" <lt*******@md-it.com>
| > > > | References: <#B**************@TK2MSFTNGP09.phx.gbl>
| > > > <#r*************@TK2MSFTNGP09.phx.gbl>
| > > > | Subject: Re: disk space remaining?
| > > > | Date: Mon, 22 Sep 2003 17:02:35 -0600
| > > > | Lines: 69
| > > > | 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: <#j*************@TK2MSFTNGP11.phx.gbl>
| > > > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > > > | NNTP-Posting-Host: 64.207.45.130
| > > > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| > > > | Xref: cpmsftngxa06.phx.gbl
| > > microsoft.public.dotnet.languages.csharp:186661
| > > > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > > > |
| > > > | Thanks Willy. I tried something similar to this and it didn't
seem
| to
| > > > work.
| > > > | I was hoping there was something in the .net libraries that
allowed
| a
| > > > direct
| > > > | function call and not the System.Management class. I'm still in a | > state
| > > > of
| > > > | shock regarding this and just can't believe you can't just do
| > something
| > > > like
| > > > | "System.IO.DirectoryInfo.GetFreeSpace" or something similar...
| > > > |
| > > > | Thanks Willy. Apparently I have to keep playing with the System | > > > Management
| > > > | class though.
| > > > |
| > > > |
| > > > | "Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in
| message
| > > > | news:#r*************@TK2MSFTNGP09.phx.gbl...
| > > > | >
| > > > | > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| > > > | news:%2****************@TK2MSFTNGP09.phx.gbl...
| > > > | > > Is there no way in .net to get disk space remaining for a
given
| > > > drive???
| > > > | I
| > > > | > > can't believe it and yet is seems to be so. If someone knows of
| a
| > > way
| > > > | to do
| > > > | > > this in the .net libraries I'd be very much appreciative.
| > > > | > >
| > > > | > > Thanks, LT.
| > > > | > >
| > > > | > >
| > > > | >
| > > > | > You could use the Management namespace classes (and WMI).
| > > > | > Here's a small sample:
| > > > | >
| > > > | > using System;
| > > > | > using System.Management;
| > > > | > class Tester {
| > > > | > public static void Main() {
| > > > | > Int64 i = GetFreeSpace("C:");
| > > > | > Console.WriteLine(i);
| > > > | > }
| > > > | >
| > > > | > public static Int64 GetFreeSpace(string logicalDrive )
| > > > | > {
| > > > | > Int64 nRet = 0;
| > > > | > // Create a query
| > > > | > String strSQL = "SELECT FreeSpace FROM Win32_LogicalDisk
WHERE
| > > > | DeviceID='" + logicalDrive + "'" ;
| > > > | > try
| > > > | > {
| > > > | > SelectQuery query = new SelectQuery(strSQL);
| > > > | > ManagementObjectSearcher searcher = new
| > > > | ManagementObjectSearcher(query);
| > > > | >
| > > > | > foreach(ManagementBaseObject drive in searcher.Get())
| > > > | > {
| > > > | > UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace | > > property
| > > > | > nRet = (Int64)u;
| > > > | > }
| > > > | >
| > > > | > }
| > > > | > catch (Exception e)
| > > > | > {
| > > > | > Console.WriteLine(e.ToString());
| > > > | > }
| > > > | > return nRet/1024; // return KB
| > > > | > } // GetFreeSpace
| > > > | >
| > > > | > }
| > > > | >
| > > > | > Willy.
| > > > | >
| > > > | >
| > > > |
| > > > |
| > > > |
| > > >
| > >
| > >
| >
| >
|
|
|

Nov 15 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.