473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

disk space remaining?

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
12 10978

"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
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

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

"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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jas Shultz | last post by:
I'm using Win2K3 Enterprise edition with the latest .NET framework installed. I have this problem with getting "out of disk space" errors. It doesn't happen all the time but it does happen. When...
1
by: Paul | last post by:
Hi: I am totally new in Sun Solaris. I am not sure someone would like to tell me how to rellocate Sun Solaris disk space where oracle installed? I installed Oracle 8i in Sun Solaris and set...
2
by: Sagar Choudhary | last post by:
Is there any exception related to disk space ? If not how else can we check the availability of the disk space in a c++ program. I know ostream helps a bit. When we try to open a file when there's...
6
by: Anthony Robinson | last post by:
I am trying to instal FixPak 4 for V8 on XP. I kept getting an error. During the install, I kept getting the "low disk space on c:" message. My C; drive has 1.4 GB of free space. I can't believe...
5
by: Yasaswi Pulavarti | last post by:
does a command like, db2 drop table tabschema.tabname when run from the Aix prompt reclaim the disk space? Are there any other options? How can we make sure the disk space is reclaimed? Thanks,...
0
by: Jas Shultz | last post by:
I'm using Win2K3 Enterprise edition with the latest .NET framework installed. I have this problem with getting "out of disk space" errors. It doesn't happen all the time but it does happen. When...
2
by: Jas Shultz | last post by:
I'm using Win2K3 Enterprise edition with the latest .NET framework installed. I have this problem with getting "out of disk space" errors. I have 35 Gigs of disk space free. It doesn't happen...
13
by: ragtag99 | last post by:
I posted this on comp.lang.asm.x86, alt.os.development, comp.arch, comp.lang.c++ Im working with windows xp professional, NTFS and programming with MASM, c++ (free compiler) or visual basic 6.0...
2
by: Lidia | last post by:
Input: A computer name on the internal network Out: Disk space total and remaining on all drives.
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.