disk space remaining?

  • Thread starter Thread starter New World Order Pigs
  • Start date Start date
N

New World Order Pigs

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.
 
New World Order Pigs said:
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.
 
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 said:
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.
 
Hi,

DirectoryInfo does not have GetFreeSpace method, because it is design for
regular directory(
It makes no sense referring FreeSpace for regular directory).

The System.Management class provided a way of retreiving the information
from WMI, the
WMI's function is monument and strong, so I think there is no need for the
Net to create a new class or
method for this.(If .Net really create a new class, it will interop with
WMI under hood)

If you still do not want to use the System.Management class, you can refer
to the FileSystemObject, like this:

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

Scripting.FileSystemObject FSO = new Scripting.FileSystemObjectClass();
string [] logDrives=System.IO.Directory.GetLogicalDrives();
for ( int i =0;i< logDrives.GetUpperBound(0)+1;i++)
{
string disk=logDrives.Substring(0,2);
string param="Win32_LogicalDisk='"+disk+"'";
Scripting.Drive thisdrive=FSO.GetDrive(logDrives);
if (thisdrive.DriveType==Scripting.DriveTypeConst.Fixed )
{
MessageBox.Show(thisdrive.Path+" 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" <[email protected]>
| References: <#[email protected]>
<#[email protected]>
| 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: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.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.
|
|
| | >
| | > > 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.
| >
| >
|
|
|
 
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 said:
Hi,

DirectoryInfo does not have GetFreeSpace method, because it is design for
regular directory(
It makes no sense referring FreeSpace for regular directory).

The System.Management class provided a way of retreiving the information
from WMI, the
WMI's function is monument and strong, so I think there is no need for the
Net to create a new class or
method for this.(If .Net really create a new class, it will interop with
WMI under hood)

If you still do not want to use the System.Management class, you can refer
to the FileSystemObject, like this:

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

Scripting.FileSystemObject FSO = new Scripting.FileSystemObjectClass();
string [] logDrives=System.IO.Directory.GetLogicalDrives();
for ( int i =0;i< logDrives.GetUpperBound(0)+1;i++)
{
string disk=logDrives.Substring(0,2);
string param="Win32_LogicalDisk='"+disk+"'";
Scripting.Drive thisdrive=FSO.GetDrive(logDrives);
if (thisdrive.DriveType==Scripting.DriveTypeConst.Fixed )
{
MessageBox.Show(thisdrive.Path+" 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" <[email protected]>
| References: <#[email protected]>
<#[email protected]>
| 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: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.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.
|
|
| | >
| | > > 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.
| >
| >
|
|
|
 
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 said:
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 said:
Hi,

DirectoryInfo does not have GetFreeSpace method, because it is design for
regular directory(
It makes no sense referring FreeSpace for regular directory).

The System.Management class provided a way of retreiving the information
from WMI, the
WMI's function is monument and strong, so I think there is no need for the
Net to create a new class or
method for this.(If .Net really create a new class, it will interop with
WMI under hood)

If you still do not want to use the System.Management class, you can refer
to the FileSystemObject, like this:

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

Scripting.FileSystemObject FSO = new Scripting.FileSystemObjectClass();
string [] logDrives=System.IO.Directory.GetLogicalDrives();
for ( int i =0;i< logDrives.GetUpperBound(0)+1;i++)
{
string disk=logDrives.Substring(0,2);
string param="Win32_LogicalDisk='"+disk+"'";
Scripting.Drive thisdrive=FSO.GetDrive(logDrives);
if (thisdrive.DriveType==Scripting.DriveTypeConst.Fixed )
{
MessageBox.Show(thisdrive.Path+" 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" <[email protected]>
| References: <#[email protected]>
<#[email protected]>
| 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: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.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.
|
|
| | >
| | > > 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.
| >
| >
|
|
|

 
OK, if I use "CIM_LogicalDisk" then everything works... Why is that? Does
anyone know and as I deploy thi
New World Order Pigs said:
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 said:
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 said:
Hi,

DirectoryInfo does not have GetFreeSpace method, because it is design for
regular directory(
It makes no sense referring FreeSpace for regular directory).

The System.Management class provided a way of retreiving the information
from WMI, the
WMI's function is monument and strong, so I think there is no need for the
Net to create a new class or
method for this.(If .Net really create a new class, it will interop with
WMI under hood)

If you still do not want to use the System.Management class, you can refer
to the FileSystemObject, like this:

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

Scripting.FileSystemObject FSO = new Scripting.FileSystemObjectClass();
string [] logDrives=System.IO.Directory.GetLogicalDrives();
for ( int i =0;i< logDrives.GetUpperBound(0)+1;i++)
{
string disk=logDrives.Substring(0,2);
string param="Win32_LogicalDisk='"+disk+"'";
Scripting.Drive thisdrive=FSO.GetDrive(logDrives);
if (thisdrive.DriveType==Scripting.DriveTypeConst.Fixed )
{
MessageBox.Show(thisdrive.Path+" 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" <[email protected]>
| References: <#[email protected]>
<#[email protected]>
| 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: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.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.
|
|
| | >
| | > > 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.
| >
| >
|
|
|

 
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 said:
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 said:
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 said:
Hi,

DirectoryInfo does not have GetFreeSpace method, because it is design for
regular directory(
It makes no sense referring FreeSpace for regular directory).

The System.Management class provided a way of retreiving the information
from WMI, the
WMI's function is monument and strong, so I think there is no need for the
Net to create a new class or
method for this.(If .Net really create a new class, it will interop with
WMI under hood)

If you still do not want to use the System.Management class, you can refer
to the FileSystemObject, like this:

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

Scripting.FileSystemObject FSO = new Scripting.FileSystemObjectClass();
string [] logDrives=System.IO.Directory.GetLogicalDrives();
for ( int i =0;i< logDrives.GetUpperBound(0)+1;i++)
{
string disk=logDrives.Substring(0,2);
string param="Win32_LogicalDisk='"+disk+"'";
Scripting.Drive thisdrive=FSO.GetDrive(logDrives);
if (thisdrive.DriveType==Scripting.DriveTypeConst.Fixed )
{
MessageBox.Show(thisdrive.Path+" 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" <[email protected]>
| References: <#[email protected]>
<#[email protected]>
| 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: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.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.
|
|
| | >
| | > > 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.
| >
| >
|
|
|

 
New World Order Pigs said:
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.
 
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.
 
Hi,

In MSDN document, you can find that Win32_LogicalDisk class is inherited
from CIM_LogicalDisk
class.

Because the ManagementObject you get from Win32_LogicalDisk not all have
the FreeSpace property
(Floppy disk and CDROM does not have this property value), you should check
every object's type before
retrieving the FreeSpace property.

Sample like this:

ManagementClass mc = new ManagementClass("Win32_LogicalDisk");
ManagementObjectCollection mco = mc.GetInstances();
foreach (ManagementObject mo in mco)
{
if(mo["DriveType"].ToString().Equals("3"))
{
MessageBox.Show( mo["FreeSpace"].ToString());
}
}

It works well on my machine, it will retrieve every hard disk's FreeSpace
on my machine.

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

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

NWOP

Jeffrey Tan said:
Hi,

In MSDN document, you can find that Win32_LogicalDisk class is inherited
from CIM_LogicalDisk
class.

Because the ManagementObject you get from Win32_LogicalDisk not all have
the FreeSpace property
(Floppy disk and CDROM does not have this property value), you should check
every object's type before
retrieving the FreeSpace property.

Sample like this:

ManagementClass mc = new ManagementClass("Win32_LogicalDisk");
ManagementObjectCollection mco = mc.GetInstances();
foreach (ManagementObject mo in mco)
{
if(mo["DriveType"].ToString().Equals("3"))
{
MessageBox.Show( mo["FreeSpace"].ToString());
}
}

It works well on my machine, it will retrieve every hard disk's FreeSpace
on my machine.

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

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

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" <[email protected]>
| > > > | References: <#[email protected]>
| > > > <#[email protected]>
| > > > | 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: <#[email protected]>
| > > > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > > > | NNTP-Posting-Host: 64.207.45.130
| > > > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.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.
| > > > |
| > > > |
| message
| > > > | | > > > | >
| > > > | | > > > | > > 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.
| > > > | >
| > > > | >
| > > > |
| > > > |
| > > > |
| > > >
| > >
| > >
| >
| >
|
|
|
 
Back
Top