tags:

views:

3548

answers:

2

Possible Duplicate:
how to get disk information by using c#?

Given each of the inputs below, I'd like to get free space on that location. Something like

long GetFreeSpace(string path)

Inputs:

c:

c:\

c:\temp

\\server

\\server\C\storage
+3  A: 

DriveInfo will help you with some of those (but it doesn't work with UNC paths), but really I think you will need to use GetDiskFreeSpaceEx. You can probably achieve some functionality with WMI. GetDiskFreeSpaceEx looks like your best bet.

Chances are you will probably have to clean up your paths to get it to work properly.

RichardOD
+1  A: 

untested:

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

Btw what is the outcome of free diskspace on c:\temp ? you will get the space free of c:\

PoweRoy
what if c:\temp is a junction/resparse point?
kenny
As Kenny says, the free space for any given directory is not necessarily the same as the free space for the drive of the root directory. Certainly isn't on my machine.
Barry Kelly