// (c) 2006 Richard Grimes // www.grimes.demon.co.uk using System; using System.Security; using System.Reflection; using System.Runtime.InteropServices; [assembly: AssemblyKeyFile("key.snk")] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AllowPartiallyTrustedCallers] public class DiskInfo { [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)] static extern bool GetDiskFreeSpaceEx( string drive, out long freeBytesForUser, out long totalBytes, out long freeBytes); [DllImport("kernel32")] static extern int SetErrorMode(int newMode); public static long AvailableFreeSpace(string disk) { int errorMode = SetErrorMode(1); long freeBytes, totalBytes, totalFreeBytes; try { if (!GetDiskFreeSpaceEx(disk, out freeBytes, out totalBytes, out totalFreeBytes)) { throw new ArgumentException( String.Format("cannot get free space for {0} 0x{1:x8}", disk, Marshal.GetLastWin32Error())); } } finally { SetErrorMode(errorMode); } return freeBytes; } }