Get free disk space : Native Windows Function « Windows « C# / CSharp Tutorial






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);
        }
    }
}
Free disk space:
    Available bytes : 33091416064
    Total # of bytes: 60003381248
    Total free bytes: 33091416064








29.10.Native Windows Function
29.10.1.Calling Native DLL Functions
29.10.2.Calling a Function with a Structure Parameter
29.10.3.Enumerate Display Monitors
29.10.4.Get Workstation information
29.10.5.Get Computer name (char * parameter)
29.10.6.Get free disk space
29.10.7.Use native windows function to read file
29.10.8.The windows version information
29.10.9.Get current Active Window
29.10.10.Writing INI file: Write Private Profile String
29.10.11.Reading INI file: Get Private Profile String
29.10.12.GetVersionEx by using kernel32.dll
29.10.13.Get computer name (StringBuilder parameter)
29.10.14.Lock work station
29.10.15.Get Monitor Information