如何确定远程驱动器是否有足够的空间让我在 .Net 中使用 C# 上传给定文件?
4 回答
有两种可能的解决方案。
调用 Win32 函数 GetDiskFreeSpaceEx。这是一个示例程序:
internal static class Win32 { [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] internal static extern bool GetDiskFreeSpaceEx(string drive, out long freeBytesForUser, out long totalBytes, out long freeBytes); } class Program { static void Main(string[] args) { long freeBytesForUser; long totalBytes; long freeBytes; if (Win32.GetDiskFreeSpaceEx(@"\\prime\cargohold", out freeBytesForUser, out totalBytes, out freeBytes)) { Console.WriteLine(freeBytesForUser); Console.WriteLine(totalBytes); Console.WriteLine(freeBytes); } } }
使用系统管理界面。这篇文章中有另一个答案描述了这一点。此方法实际上是为在 PowerShell 等脚本语言中使用而设计的。为了得到正确的对象,它执行了很多绒毛。最终,我怀疑这种方法归结为调用 GetDiskFreeSpaceEx。
任何使用 C# 进行任何严肃的 Windows 开发的人最终都可能会调用许多 Win32 函数。.NET 框架并没有 100% 覆盖 Win32 API。任何大型程序都会很快发现 .NET 库中的空白,这些空白只能通过 Win32 API 获得。我会得到一个 .NET 的 Win32 包装器,并将其包含在您的项目中。这将使您可以即时访问几乎所有 Win32 API。
使用 WMI
using System.Management;
// Get all the network drives (drivetype=4)
SelectQuery query = new SelectQuery("select Name, VolumeName, FreeSpace from win32_logicaldisk where drivetype=4");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject drive in searcher.Get())
{
string Name = (string)drive["Name"];
string VolumeName = (string)drive["VolumeName"];
UInt64 freeSpace = (UInt64)drive["FreeSpace"];
}
您是在谈论将网络共享映射到计算机上的逻辑驱动器吗?
如果是这样,您可以使用 DriveInfo。
DriveInfo info = new DriveInfo("X:"); info.AvailableFreeSpace;
DriveInfo 仅适用于逻辑驱动器,因此如果您仅使用完整共享 (UNC) 名称,我认为上述代码不会起作用。
我不确定 GetDiskFreeSpaceEx 是否适用于 UNC 共享,但如果它确实使用它,那么这里是如何将 UNC 共享安装到 logal 驱动器:
编辑GetDiskFreeSpaceEx 确实适用于 UNC 共享,使用它......但是,这个代码太费力了,无法删除,如果您想在代码中将 UNC 共享挂载为本地驱动器,它会很方便。
public class DriveWrapper
{
[StructLayout(LayoutKind.Sequential)]
public struct NETRESOURCEA
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
[MarshalAs(UnmanagedType.LPStr)]
public string lpLocalName;
[MarshalAs(UnmanagedType.LPStr)]
public string lpRemoteName;
[MarshalAs(UnmanagedType.LPStr)]
public string lpComment;
[MarshalAs(UnmanagedType.LPStr)]
public string lpProvider;
public override String ToString()
{
String str = "LocalName: " + lpLocalName + " RemoteName: " + lpRemoteName
+ " Comment: " + lpComment + " lpProvider: " + lpProvider;
return (str);
}
}
[DllImport("mpr.dll")]
public static extern int WNetAddConnection2A(
[MarshalAs(UnmanagedType.LPArray)] NETRESOURCEA[] lpNetResource,
[MarshalAs(UnmanagedType.LPStr)] string lpPassword,
[MarshalAs(UnmanagedType.LPStr)] string UserName,
int dwFlags);
[DllImport("mpr.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern int WNetCancelConnection2A(
[MarshalAs(UnmanagedType.LPStr)]
string lpName,
int dwFlags,
int fForce
);
public int GetDriveSpace(string shareName, string userName, string password)
{
NETRESOURCEA[] n = new NETRESOURCEA[1];
n[0] = new NETRESOURCEA();
n[0].dwScope = 0;
n[0].dwType = 0;
n[0].dwDisplayType = 0;
n[0].dwUsage = 0;
n[0].dwType = 1;
n[0].lpLocalName = "x:";
n[0].lpRemoteName = shareName;
n[0].lpProvider = null;
int res = WNetAddConnection2A(n, userName, password, 1);
DriveInfo info = new DriveInfo("x:");
int space = info.AvailableFreeSpace;
int err = 0;
err = WNetCancelConnection2A("x:", 0, 1);
return space;
}
}