Thread: ASP.NET/C# GetDiskFreeSpace UNC

C# GetDiskFreeSpace UNC

www.pcreview.co.uk/forums/thread-1351468.php


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



using System ;

using System.Runtime.InteropServices;



namespace DiskFreeSpaceEx

{

class FreeSpace

{

[DllImport("kernel32")]

public static extern int GetDiskFreeSpace(

string lpRootPathName,

out int lpSectorsPerCluster,

out int lpBytesPerSector,

out int lpNumberOfFreeClusters,

out int lpTotalNumberOfClusters

);



[DllImport("kernel32")]

public static extern int GetDiskFreeSpaceEx(

string lpDirectoryName,

ref long lpFreeBytesAvailable,

ref long lpTotalNumberOfBytes,

ref long lpTotalNumberOfFreeBytes

);



[STAThread]

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,

lpTotalNumberOfClusters);



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.