リモート ドライブに、.Net で C# を使用して特定のファイルをアップロードするのに十分なスペースがあるかどうかを判断するにはどうすればよいですか?
4 に答える
考えられる解決策は 2 つあります。
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 フレームワークは、Win32 API の 100% をカバーしていません。大規模なプログラムでは、Win32 API でしか利用できない .NET ライブラリのギャップがすぐに見つかります。.NET 用の Win32 ラッパーの 1 つを入手し、これをプロジェクトに含めます。これにより、ほぼすべての 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"];
}
GetDiskFreeSpaceEx が UNC 共有で機能するかどうかはわかりませんが、それを使用する場合は、UNC 共有をログ ドライブにマウントする方法を次に示します。
編集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;
}
}
ネットワーク共有をコンピューターの論理ドライブにマッピングすることについて話しているのですか?
その場合は、DriveInfo を使用できます。
DriveInfo info = new DriveInfo("X:"); info.AvailableFreeSpace;
DriveInfo は論理ドライブでのみ機能するため、完全な共有 (UNC) 名を使用しているだけの場合、上記のコードは機能しないと思います。