As seen in Get available disk free space for a given path on Windows :
Use the winapi function GetDiskFreeSpaceEx
to determine free space on a UNC (network) path. For example, create a new VS Project called FreeSpace and paste this as Program.cs:
using System;
using System.Runtime.InteropServices;
namespace FreeSpace
{
class Program
{
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
static void Main(string[] args)
{
ulong FreeBytesAvailable;
ulong TotalNumberOfBytes;
ulong TotalNumberOfFreeBytes;
bool success = GetDiskFreeSpaceEx(@"\\NETSHARE\folder",
out FreeBytesAvailable,
out TotalNumberOfBytes,
out TotalNumberOfFreeBytes);
if (!success)
throw new System.ComponentModel.Win32Exception();
Console.WriteLine("Free Bytes Available: {0,15:D}", FreeBytesAvailable);
Console.WriteLine("Total Number Of Bytes: {0,15:D}", TotalNumberOfBytes);
Console.WriteLine("Total Number Of FreeBytes: {0,15:D}", TotalNumberOfFreeBytes);
Console.ReadKey();
}
}
}
As you can see, this is the exact same code as in the Question linked above, just factored into a class plus the correct using
directives to compile without error. All credits go to https://stackoverflow.com/users/995926/rekire
WMI doesn't seem to handle free space on network shares.
But for local disks, Windows Management Interface is the way to go:
https://msdn.microsoft.com/en-us/library/aa394592(v=vs.85).aspx