我正在尝试找到一种方法来确定.NET应用程序中任意文件夹中的总磁盘空间和可用磁盘空间。通过文件夹中的“总磁盘空间”和“可用磁盘空间”,我指的是如果您对文件夹执行了“ dir”命令,该文件夹将报告的总磁盘空间和可用磁盘空间,即包含该文件夹的逻辑驱动器,并考虑在其下发出请求的用户帐户。
我正在使用C#。该方法应适用于以UNC路径(而不是通过映射的驱动器号访问)指定的本地和远程文件夹。例如,它适用于:
我从DirectoryInfo对象开始,但这似乎没有关联的磁盘空间信息。 DriveInfo类可以,但不适用于远程文件夹。
Edit。与大家进行了一些交流之后,我正在考虑将远程文件夹映射为本地驱动器,使用DriveInfo来获取数据,然后再次取消映射。这种方法的问题在于,我的应用每天需要几次收集超过120个文件夹的数据。我不确定这是否可行。
有什么想法吗?谢谢。
使用this link from MSDN类的System.IO.DriveInfo
怎么样?
您可以使用GetDiskFreeSpaceEx from kernel32.dll与UNC路径和驱动器一起使用。您需要做的只是包括一个DllImport(请参阅示例链接)。
[may不是您想要的,但是我正在努力提供帮助,它具有稍微安全地擦除驱动器的可用空间的好处。
public static string DriveSizeAvailable(string path)
{
long count = 0;
byte toWrite = 1;
try
{
using (StreamWriter writer = new StreamWriter(path))
{
while (true)
{
writer.Write(toWrite);
count++;
}
}
}
catch (IOException)
{
}
return string.Format("There used to be {0} bytes available on drive {1}.", count, path);
}
public static string DriveSizeTotal(string path)
{
DeleteAllFiles(path);
int sizeAvailable = GetAvailableSize(path);
return string.Format("Drive {0} will hold a total of {1} bytes.", path, sizeAvailable);
}
并不是真正的C#示例,但可能会给您一个提示-一个VB.NET函数,它沿指定路径返回驱动器上的可用空间和总空间(以字节为单位)。与System.IO.DriveInfo不同,它也适用于UNC路径。
VB.NET:
<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
If Not String.IsNullOrEmpty(folderName) Then
If Not folderName.EndsWith("\") Then
folderName += "\"
End If
Dim free As ULong = 0, total As ULong = 0, dummy2 As ULong = 0
If GetDiskFreeSpaceEx(folderName, free, total, dummy2) Then
freespace = free
totalspace = total
Return True
End If
End If
End Function
System.IO.DriveInfo正常工作。我连接到两个单独的Netware服务器,并映射了多个驱动器。
这里是本地C:驱动器:
Drive C:\
File type: Fixed
Volume label: Drive C
File system: NTFS
Available space to current user: 158558248960 bytes
Total available space: 158558248960 bytes
Total size of drive: 249884004352 bytes
这是其中一个网络驱动器的输出:
Drive F:\
File type: Network
Volume label: SYS
File system: NWFS
Available space to current user: 1840656384 bytes
Total available space: 1840656384 bytes
Total size of drive: 4124475392 bytes
我直接从DriveInfo上的MSDN文档中使用了以下代码:
使用系统;使用System.IO;课堂测试{公共静态void Main(){DriveInfo [] allDrives = DriveInfo.GetDrives();foreach(所有驱动器中的DriveInfo d){Console.WriteLine(“ Drive {0}”,d.Name);Console.WriteLine(“文件类型:{0}”,d.DriveType);如果(d.IsReady == true){Console.WriteLine(“卷标:{0}”,d.VolumeLabel);Console.WriteLine(“文件系统:{0}”,d.DriveFormat);Console.WriteLine(“当前用户的可用空间:{0,15}字节”,d.AvailableFreeSpace);Console.WriteLine(“总可用空间:{0,15}字节”,d.TotalFreeSpace);Console.WriteLine(“驱动器总大小:{0,15}字节”,d.TotalSize);}}}}
这是我使用多年的另一种可能性。下面的示例是VBScript,但是它可以与任何支持COM的语言一起使用。请注意,GetDrive()
也适用于UNC共享。
Dim Tripped
Dim Level
Tripped = False
Level = 0
Sub Read(Entry, Source, SearchText, Minimum, Maximum)
Dim fso
Dim disk
Set fso = CreateObject("Scripting.FileSystemObject")
Set disk = fso.GetDrive(Source)
Level = (disk.AvailableSpace / (1024 * 1024 * 1024))
If (CDbl(Level) < CDbl(Minimum)) or (CDbl(Level) > CDbl(Maximum)) Then
Tripped = True
Else
Tripped = False
End If
End Sub
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#应该不是问题。
我很确定这是不可能的。在Windows资源管理器中,如果我尝试获取UNC目录的文件夹属性,则在可用空间上没有任何帮助。已用/可用空间是驱动器而不是文件夹的特征,并且UNC共享仅被视为文件夹。
您必须:-映射驱动器-在远程计算机上运行某些命令以检查磁盘空间。
[您还可能遇到诸如分布式文件系统之类的问题,在该系统中,UNC /映射的共享未绑定到任何特定的驱动器,因此您实际上必须汇总几个驱动器。
关于用户配额呢?驱动器可能未满,但是您用于写入该文件夹的帐户可能已达到极限。
不是C#,仅提供应有的空格,而是。 。 。
dir \\server\share | find /i "bytes free"
让您参与其中。我正在寻找相同的解决方案,但似乎没有一个好的解决方案-特别是在尝试避免映射驱动器时。