如Get available disk free space for a given path on Windows 中所示:
使用 winapi 函数GetDiskFreeSpaceEx
确定 UNC(网络)路径上的可用空间。例如,创建一个名为 FreeSpace 的新 VS 项目并将其粘贴为 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();
}
}
}
如您所见,这与上面链接的问题中的代码完全相同,只是将其分解为一个类加上正确的using
指令以进行无错误编译。所有学分都转到https://stackoverflow.com/users/995926/rekire
WMI 似乎不处理网络共享上的可用空间。但是对于本地磁盘,Windows 管理界面是要走的路:
https ://msdn.microsoft.com/en-us/library/aa394592(v=vs.85).aspx