473,883 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11039

"New World Order Pigs" <lt*******@md-it.com> wrote in message news:%2******** ********@TK2MSF TNGP09.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.Manageme nt;
class Tester {
public static void Main() {
Int64 i = GetFreeSpace("C :");
Console.WriteLi ne(i);
}

public static Int64 GetFreeSpace(st ring logicalDrive )
{
Int64 nRet = 0;
// Create a query
String strSQL = "SELECT FreeSpace FROM Win32_LogicalDi sk WHERE DeviceID='" + logicalDrive + "'" ;
try
{
SelectQuery query = new SelectQuery(str SQL);
ManagementObjec tSearcher searcher = new ManagementObjec tSearcher(query );

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

}
catch (Exception e)
{
Console.WriteLi ne(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.Manageme nt 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.Dire ctoryInfo.GetFr eeSpace" 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******** *****@TK2MSFTNG P09.phx.gbl...

"New World Order Pigs" <lt*******@md-it.com> wrote in message news:%2******** ********@TK2MSF TNGP09.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.Manageme nt;
class Tester {
public static void Main() {
Int64 i = GetFreeSpace("C :");
Console.WriteLi ne(i);
}

public static Int64 GetFreeSpace(st ring logicalDrive )
{
Int64 nRet = 0;
// Create a query
String strSQL = "SELECT FreeSpace FROM Win32_LogicalDi sk WHERE

DeviceID='" + logicalDrive + "'" ; try
{
SelectQuery query = new SelectQuery(str SQL);
ManagementObjec tSearcher searcher = new ManagementObjec tSearcher(query );
foreach(Managem entBaseObject drive in searcher.Get())
{
UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace property
nRet = (Int64)u;
}

}
catch (Exception e)
{
Console.WriteLi ne(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.Manageme nt 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.Manageme nt class, you can refer
to the FileSystemObjec t, like this:

Add the "Microsoft Scripting RunTIme 1.0" from the "Add Reference" command,
"COM" tab page,

Scripting.FileS ystemObject FSO = new Scripting.FileS ystemObjectClas s();
string [] logDrives=Syste m.IO.Directory. GetLogicalDrive s();
for ( int i =0;i< logDrives.GetUp perBound(0)+1;i ++)
{
string disk=logDrives[i].Substring(0,2) ;
string param="Win32_Lo gicalDisk='"+di sk+"'";
Scripting.Drive thisdrive=FSO.G etDrive(logDriv es[i]);
if (thisdrive.Driv eType==Scriptin g.DriveTypeCons t.Fixed )
{
MessageBox.Show (thisdrive.Path +" FreeSpace:"+thi sdrive.FreeSpac e );
}
}

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.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1866 61
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.Manageme nt 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.Dire ctoryInfo.GetFr eeSpace" 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******** *****@TK2MSFTNG P09.phx.gbl...
| >
| > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| news:%2******** ********@TK2MSF TNGP09.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.Manageme nt;
| > class Tester {
| > public static void Main() {
| > Int64 i = GetFreeSpace("C :");
| > Console.WriteLi ne(i);
| > }
| >
| > public static Int64 GetFreeSpace(st ring logicalDrive )
| > {
| > Int64 nRet = 0;
| > // Create a query
| > String strSQL = "SELECT FreeSpace FROM Win32_LogicalDi sk WHERE
| DeviceID='" + logicalDrive + "'" ;
| > try
| > {
| > SelectQuery query = new SelectQuery(str SQL);
| > ManagementObjec tSearcher searcher = new
| ManagementObjec tSearcher(query );
| >
| > foreach(Managem entBaseObject drive in searcher.Get())
| > {
| > UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace property
| > nRet = (Int64)u;
| > }
| >
| > }
| > catch (Exception e)
| > {
| > Console.WriteLi ne(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_Logical Disk");
ManagementObjec tCollection mco = mc.GetInstances ();
foreach (ManagementObje ct 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.mi crosoft.com> wrote in message
news:Jm******** ******@cpmsftng xa06.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.Manageme nt 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.Manageme nt class, you can refer
to the FileSystemObjec t, like this:

Add the "Microsoft Scripting RunTIme 1.0" from the "Add Reference" command, "COM" tab page,

Scripting.FileS ystemObject FSO = new Scripting.FileS ystemObjectClas s();
string [] logDrives=Syste m.IO.Directory. GetLogicalDrive s();
for ( int i =0;i< logDrives.GetUp perBound(0)+1;i ++)
{
string disk=logDrives[i].Substring(0,2) ;
string param="Win32_Lo gicalDisk='"+di sk+"'";
Scripting.Drive thisdrive=FSO.G etDrive(logDriv es[i]);
if (thisdrive.Driv eType==Scriptin g.DriveTypeCons t.Fixed )
{
MessageBox.Show (thisdrive.Path +" FreeSpace:"+thi sdrive.FreeSpac e );
}
}

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.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1866 61 | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.Manageme nt 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.Dire ctoryInfo.GetFr eeSpace" 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******** *****@TK2MSFTNG P09.phx.gbl...
| >
| > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| news:%2******** ********@TK2MSF TNGP09.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.Manageme nt;
| > class Tester {
| > public static void Main() {
| > Int64 i = GetFreeSpace("C :");
| > Console.WriteLi ne(i);
| > }
| >
| > public static Int64 GetFreeSpace(st ring logicalDrive )
| > {
| > Int64 nRet = 0;
| > // Create a query
| > String strSQL = "SELECT FreeSpace FROM Win32_LogicalDi sk WHERE
| DeviceID='" + logicalDrive + "'" ;
| > try
| > {
| > SelectQuery query = new SelectQuery(str SQL);
| > ManagementObjec tSearcher searcher = new
| ManagementObjec tSearcher(query );
| >
| > foreach(Managem entBaseObject drive in searcher.Get())
| > {
| > UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace property | > nRet = (Int64)u;
| > }
| >
| > }
| > catch (Exception e)
| > {
| > Console.WriteLi ne(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******** *****@TK2MSFTNG P10.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_Logical Disk");
ManagementObjec tCollection mco = mc.GetInstances ();
foreach (ManagementObje ct 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.mi crosoft.com> wrote in message
news:Jm******** ******@cpmsftng xa06.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.Manageme nt 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.Manageme nt class, you can refer to the FileSystemObjec t, like this:

Add the "Microsoft Scripting RunTIme 1.0" from the "Add Reference"

command,
"COM" tab page,

Scripting.FileS ystemObject FSO = new Scripting.FileS ystemObjectClas s();
string [] logDrives=Syste m.IO.Directory. GetLogicalDrive s();
for ( int i =0;i< logDrives.GetUp perBound(0)+1;i ++)
{
string disk=logDrives[i].Substring(0,2) ;
string param="Win32_Lo gicalDisk='"+di sk+"'";
Scripting.Drive thisdrive=FSO.G etDrive(logDriv es[i]);
if (thisdrive.Driv eType==Scriptin g.DriveTypeCons t.Fixed )
{
MessageBox.Show (thisdrive.Path +" FreeSpace:"+thi sdrive.FreeSpac e );
}
}

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.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl

microsoft.publi c.dotnet.langua ges.csharp:1866 61
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.Manageme nt 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.Dire ctoryInfo.GetFr eeSpace" 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******** *****@TK2MSFTNG P09.phx.gbl...
| >
| > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| news:%2******** ********@TK2MSF TNGP09.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.Manageme nt;
| > class Tester {
| > public static void Main() {
| > Int64 i = GetFreeSpace("C :");
| > Console.WriteLi ne(i);
| > }
| >
| > public static Int64 GetFreeSpace(st ring logicalDrive )
| > {
| > Int64 nRet = 0;
| > // Create a query
| > String strSQL = "SELECT FreeSpace FROM Win32_LogicalDi sk WHERE
| DeviceID='" + logicalDrive + "'" ;
| > try
| > {
| > SelectQuery query = new SelectQuery(str SQL);
| > ManagementObjec tSearcher searcher = new
| ManagementObjec tSearcher(query );
| >
| > foreach(Managem entBaseObject drive in searcher.Get())
| > {
| > UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace

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


Nov 15 '05 #6
OK, if I use "CIM_LogicalDis k" 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******** ******@TK2MSFTN GP09.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******** *****@TK2MSFTNG P10.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_Logical Disk");
ManagementObjec tCollection mco = mc.GetInstances ();
foreach (ManagementObje ct 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.mi crosoft.com> wrote in message
news:Jm******** ******@cpmsftng xa06.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.Manageme nt 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.Manageme nt class, you can refer to the FileSystemObjec t, like this:

Add the "Microsoft Scripting RunTIme 1.0" from the "Add Reference"

command,
"COM" tab page,

Scripting.FileS ystemObject FSO = new Scripting.FileS ystemObjectClas s(); string [] logDrives=Syste m.IO.Directory. GetLogicalDrive s();
for ( int i =0;i< logDrives.GetUp perBound(0)+1;i ++)
{
string disk=logDrives[i].Substring(0,2) ;
string param="Win32_Lo gicalDisk='"+di sk+"'";
Scripting.Drive thisdrive=FSO.G etDrive(logDriv es[i]);
if (thisdrive.Driv eType==Scriptin g.DriveTypeCons t.Fixed )
{
MessageBox.Show (thisdrive.Path +" FreeSpace:"+thi sdrive.FreeSpac e );
}
}

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.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl

microsoft.publi c.dotnet.langua ges.csharp:1866 61
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.Manageme nt 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.Dire ctoryInfo.GetFr eeSpace" 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******** *****@TK2MSFTNG P09.phx.gbl...
| >
| > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| news:%2******** ********@TK2MSF TNGP09.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.Manageme nt;
| > class Tester {
| > public static void Main() {
| > Int64 i = GetFreeSpace("C :");
| > Console.WriteLi ne(i);
| > }
| >
| > public static Int64 GetFreeSpace(st ring logicalDrive )
| > {
| > Int64 nRet = 0;
| > // Create a query
| > String strSQL = "SELECT FreeSpace FROM Win32_LogicalDi sk WHERE
| DeviceID='" + logicalDrive + "'" ;
| > try
| > {
| > SelectQuery query = new SelectQuery(str SQL);
| > ManagementObjec tSearcher searcher = new
| ManagementObjec tSearcher(query );
| >
| > foreach(Managem entBaseObject drive in searcher.Get())
| > {
| > UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace

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



Nov 15 '05 #7
I have discovered that "CIM_LogicalDis k" does work the way everyone else
seems to use "WIN32_LogicalD rive"-- 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******** ******@TK2MSFTN GP09.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******** *****@TK2MSFTNG P10.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_Logical Disk");
ManagementObjec tCollection mco = mc.GetInstances ();
foreach (ManagementObje ct 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.mi crosoft.com> wrote in message
news:Jm******** ******@cpmsftng xa06.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.Manageme nt 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.Manageme nt class, you can refer to the FileSystemObjec t, like this:

Add the "Microsoft Scripting RunTIme 1.0" from the "Add Reference"

command,
"COM" tab page,

Scripting.FileS ystemObject FSO = new Scripting.FileS ystemObjectClas s(); string [] logDrives=Syste m.IO.Directory. GetLogicalDrive s();
for ( int i =0;i< logDrives.GetUp perBound(0)+1;i ++)
{
string disk=logDrives[i].Substring(0,2) ;
string param="Win32_Lo gicalDisk='"+di sk+"'";
Scripting.Drive thisdrive=FSO.G etDrive(logDriv es[i]);
if (thisdrive.Driv eType==Scriptin g.DriveTypeCons t.Fixed )
{
MessageBox.Show (thisdrive.Path +" FreeSpace:"+thi sdrive.FreeSpac e );
}
}

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.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl

microsoft.publi c.dotnet.langua ges.csharp:1866 61
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.Manageme nt 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.Dire ctoryInfo.GetFr eeSpace" 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******** *****@TK2MSFTNG P09.phx.gbl...
| >
| > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| news:%2******** ********@TK2MSF TNGP09.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.Manageme nt;
| > class Tester {
| > public static void Main() {
| > Int64 i = GetFreeSpace("C :");
| > Console.WriteLi ne(i);
| > }
| >
| > public static Int64 GetFreeSpace(st ring logicalDrive )
| > {
| > Int64 nRet = 0;
| > // Create a query
| > String strSQL = "SELECT FreeSpace FROM Win32_LogicalDi sk WHERE
| DeviceID='" + logicalDrive + "'" ;
| > try
| > {
| > SelectQuery query = new SelectQuery(str SQL);
| > ManagementObjec tSearcher searcher = new
| ManagementObjec tSearcher(query );
| >
| > foreach(Managem entBaseObject drive in searcher.Get())
| > {
| > UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace

property
| > nRet = (Int64)u;
| > }
| >
| > }
| > catch (Exception e)
| > {
| > Console.WriteLi ne(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******** ******@TK2MSFTN GP10.phx.gbl...
I have discovered that "CIM_LogicalDis k" does work the way everyone else
seems to use "WIN32_LogicalD rive"-- 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_LogicalDis k" instead of
"Win32_LogicalD isk." 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******** ******@TK2MSFTN GP11.phx.gbl...

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

news:Oq******** ******@TK2MSFTN GP10.phx.gbl...
I have discovered that "CIM_LogicalDis k" does work the way everyone else
seems to use "WIN32_LogicalD rive"-- 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6314
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 I installed Sharepoint Services I got the error and had to reboot and then everything was fine. I get it sometime when running aspx pages. It's very odd. Usually, I can just remove the application and readd it from the properties menu in the IIS...
1
4502
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 Oracle main table space called TABLES in unix disk space /dpp/live01. From the following, it's quite easy to see that the disk space of /dpp/live01 is 97%.Therefore how can i move some disk space of SWAP to /dpp/live01 without losing
2
3241
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 no disk space ostream::rdstate() returns badbit.
6
2082
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 that installing a fixpak (temp files and all) would take up over aGB of space - but it is!!! How can I get this installed? All the programs that are on my machine need to be there, so there's not the option of deleting stuff or removing...
5
7597
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, Yasaswi
0
2498
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 I installed Sharepoint Services I got the error and had to reboot and then everything was fine. I get it sometime when running aspx pages. It's very odd. Usually, I can just remove the application and readd it from the properties menu in the IIS...
2
16080
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 all the time but it does happen. When I installed Sharepoint Services I got the error and had to reboot and then everything was fine. I get it sometime when running aspx pages. It's very odd. Usually, I can just remove the application and readd it...
13
4889
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 === question 1 Primarily Im trying to design a program that has full control over a hard disk. What it needs to do is find out what sectors haven't been
2
1713
by: Lidia | last post by:
Input: A computer name on the internal network Out: Disk space total and remaining on all drives.
0
9798
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10767
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10422
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9588
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7980
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5807
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6008
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3242
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.