GetFree Dsik Space

  • Thread starter Thread starter René Sørensen
  • Start date Start date
R

René Sørensen

How do i get the free diskspace from a windows logic drive like C:\
and a network path like \\file-server??

Rene
 
Q: How to get the disk size and the free disk space?

A:
using System;
using System.Management;

....

ManagementObject disk = new
ManagementObject("win32_logicaldisk.deviceid="c:"");
disk.Get();
Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes");
Console.WriteLine("Logical Disk FreeSpace = " + disk["FreeSpace"] + "
bytes");
 
SerhaD Inanli sry i asked that question again, my post didn't came up with
my news reader, so thought it was me not posting it right.

The code you send is working, with logic drives, but i can't seem to figure
out how to it with a network path like "\\file-srever\appz\".



Eyal Safran gave me this code,




using System.Collections;
using System.Management;



ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT *
From Win32_LogicalDisk ");
ManagementObjectCollection queryCollection = query.Get();

//loop throught each object to get drive information
foreach ( ManagementObject mo in queryCollection)

switch (int.Parse( mo["DriveType"].ToString()))

case 2: //removable drives

case 3: //Local drives
case 4: //Network drives
Console.WriteLine("Drive: {0}, FreeSpace: {1}", mo["Name"],
mo["FreeSpace"]);

case 5: //CD rom drives

case 6: //Ram Disk

default: //defalut to folder

}



This code is great to, and working if I have a mounted net work drive.

I have a dynamic network path like \\file-server\appz or maybe
\\file-server\user. And I have a function to calculate the size and a
function to get the size from the disk, like shown below here.

How do I get the size of this path GetFreeSpace("\\\\file-server\\appz");



public string GetFreeSpace( string drive )

{



ManagementObject disk = new
ManagementObject("win32_logicaldisk.deviceid=\"" + drive +""\"");



disk.Get();



return this.calc( Convert.ToDouble(
disk["Size"] ) );

}



private string calc( double size )

{

string[] sizes = new
string[5]{"B","Kb","Mb","Gb","Tb"};

int i = 0;





// calculate the size.

// If the size is over 1024 bytes, the Kb will be
add as the extension, and so on

while ( size >= 1024 && ( i < sizes.Length - 1) )

{

size /= 1024;

i++;

}



size = System.Math.Round( size, 2 );



// return the size as a string

return size.ToString() + " " + sizes[ i ];

}
 
SerhaD said:
Q: How to get the disk size and the free disk space?

A:
using System;
using System.Management;

Yuck!

This is the way that Microsoft recommends, but it really is horrible.
The reason is that it means that you'll have an interprocess call to the
WMI service, using COM interop. That's a lot of CPU cycles. It's far
quicker to use platform invoke and call GetDiskFreeSpaceEx.

Richard
 
Richard Grimes said:
Yuck!

This is the way that Microsoft recommends, but it really is horrible. The
reason is that it means that you'll have an interprocess call to the WMI
service, using COM interop. That's a lot of CPU cycles. It's far quicker
to use platform invoke and call GetDiskFreeSpaceEx.

Richard

Nonsense, this is not a query for a local drive's free space, its network
share free diskspace query , this involves a network hop anyway!
So, who cares if it takes 30 msec or 10 msec if you only need to call this
once every say 10 minutes?

Here is what I have measured over a local 100MB LAN.

using System.Management
Freespace 36523753472 on \\xxxxx\ZDRIVE
Total elapsed time: 27ms

using native C calling GetDiskFreeSpaceEx
Freespace 36523753472 on \\xxxxx\ZDRIVE
Total elapsed time: 9ms

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top