Maksim Sestic は、ローカル パスと UNC パスの両方で機能するため、最良の回答を提供しています。エラー処理を改善するために彼のコードを少し変更し、例を含めました。魅力のように私のために働きます。
あなたが置く必要があります
Imports System.Runtime.InteropServices
コードに追加して、DllImport を認識できるようにします。
変更されたコードは次のとおりです。
<DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetDiskFreeSpaceEx(lpDirectoryName As String, ByRef lpFreeBytesAvailable As ULong, ByRef lpTotalNumberOfBytes As ULong, ByRef lpTotalNumberOfFreeBytes As ULong) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Public Shared Function GetDriveSpace(folderName As String, ByRef freespace As ULong, ByRef totalspace As ULong) As Boolean
Dim free As ULong = 0
Dim total As ULong = 0
Dim dummy2 As ULong = 0
Try
If Not String.IsNullOrEmpty(folderName) Then
If Not folderName.EndsWith("\") Then
folderName += "\"
End If
If GetDiskFreeSpaceEx(folderName, free, total, dummy2) Then
freespace = free
totalspace = total
Return True
End If
End If
Catch
End Try
Return False
End Function
次のように呼び出します。
dim totalspace as ulong = 0
dim freespace as ulong = 0
if GetDriveSpace("\\anycomputer\anyshare", freespace, totalspace) then
'do what you need to do with freespace and totalspace
else
'some error
end if
フォルダ名は、次のようなローカル ディレクトリにすることもできますdrive:\path\path\...
これはまだ VB.NET にありますが、C# に変換するのに問題はありません。