Get free disk space : Native Windows Function : Windows C# Examples


C# Examples » Windows » Native Windows Function »

 

Get free disk space









    
using  System;
using  System.IO;
using  System.Reflection;
using  System.Runtime.ConstrainedExecution;
using  System.Runtime.InteropServices;
using  Microsoft.Win32.SafeHandles;


public  class  MainClass
{
        [DllImport("kernel32.dll",  SetLastError  =  true,  CharSet  =  CharSet.Auto)]
        static  extern  bool  GetDiskFreeSpaceEx(string  lpDirectoryName,
              out  ulong  lpFreeBytesAvailable,
              out  ulong  lpTotalNumberOfBytes,
              out  ulong  lpTotalNumberOfFreeBytes);
        public  static  void  Main()
        {
                ulong  freeBytesAvail;
                ulong  totalNumOfBytes;
                ulong  totalNumOfFreeBytes;

                if  (!GetDiskFreeSpaceEx("C:\\",  out  freeBytesAvail,  out  totalNumOfBytes,  out  totalNumOfFreeBytes))
                {
                        Console.Error.WriteLine("Error  occurred:  {0}",
                                Marshal.GetExceptionForHR(Marshal.GetLastWin32Error()).Message);
                }
                else
                {
                        Console.WriteLine("Free  disk  space:");
                        Console.WriteLine("        Available  bytes  :  {0}",  freeBytesAvail);
                        Console.WriteLine("        Total  #  of  bytes:  {0}",  totalNumOfBytes);
                        Console.WriteLine("        Total  free  bytes:  {0}",  totalNumOfFreeBytes);
                }
        }
}
    
   
  
   



Output

Free disk space:
    Available bytes : 33091416064
    Total # of bytes: 60003381248
    Total free bytes: 33091416064


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Windows
» Native Windows Function