Thread: ASP.NET/C# GetDiskFreeSpace UNC

C# GetDiskFreeSpace UNC

(Note: change the "ServerName" and "ShareName" to yours)

using System ;

using System.Runtime.InteropServices;

namespace DiskFreeSpaceEx


class FreeSpace



public static extern int GetDiskFreeSpace(

string lpRootPathName,

out int lpSectorsPerCluster,

out int lpBytesPerSector,

out int lpNumberOfFreeClusters,

out int lpTotalNumberOfClusters



public static extern int GetDiskFreeSpaceEx(

string lpDirectoryName,

ref long lpFreeBytesAvailable,

ref long lpTotalNumberOfBytes,

ref long lpTotalNumberOfFreeBytes



static void Main(string[] args)


string lpRootPathName = @"\\ServerName\ShareName\";

int lpSectorsPerCluster;

int lpBytesPerSector;

int lpNumberOfFreeClusters;

int lpTotalNumberOfClusters;

int bRC = GetDiskFreeSpace(lpRootPathName, out lpSectorsPerCluster, out

lpBytesPerSector, out lpNumberOfFreeClusters, out lpTotalNumberOfClusters);

Console.WriteLine( "{0}\t{1}\t{2}\t{3}\t{4}", lpRootPathName,

lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters,


string lpDirectoryName = @"\\ServerName\ShareName\";

long lpFreeBytesAvailable = 0;

long lpTotalNumberOfBytes = 0;

long lpTotalNumberOfFreeBytes = 0;

bRC = GetDiskFreeSpaceEx(lpDirectoryName, ref lpFreeBytesAvailable, ref

lpTotalNumberOfBytes, ref lpTotalNumberOfFreeBytes);

Console.WriteLine( "{0}\t{1}\t{2}\t{3}", lpDirectoryName,

lpFreeBytesAvailable, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes);




If you still get zeros, check that you have adequate permissions on the

Server\Share, I had to RunAs Administrator to get UNC paths to work.



-- 13/08/2009 13:04:44: post edited by sergey.