Finding out total and free disk space in .NET

前端 未结 9 1191
我在风中等你
我在风中等你 2021-01-04 03:08

I am trying to find a way to determine the total and available disk space in an arbitrary folder from a .NET app. By \"total disk space\" and \"available disk space\" in a f

相关标签:
9条回答
  • 2021-01-04 03:42

    Maksim Sestic has given the best answer, as it works on both, local and UNC paths. I have changed his code a little for better error handling and included an example. Works for me like a charm.

    You need to put

    Imports System.Runtime.InteropServices
    

    into your code, to allow DllImport to be recognized.

    Here is the modified code:

    <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
    

    Call it this way:

    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
    

    The foldername can also be a local directory like drive:\path\path\...

    It is still in VB.NET but shouldn't be a problem to translate into C#.

    0 讨论(0)
  • 2021-01-04 03:43

    Here's one more possibility that I've used for years. The example below is VBScript, but it should work with any COM-aware language. Note that GetDrive() works on UNC shares as well.

    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
    
    0 讨论(0)
  • 2021-01-04 03:45

    I'm pretty sure this is impossible. In windows explorer, if I try to get the folder properties of a UNC directory, it gives me nothing as far as available space. Used/Available space is a characteristic of drives, not folders, and UNC shares are treated as just folders.

    you have to either:
    - Map a drive
    - Run something on the remote machine to check disk space.

    You could also run into problems with something like Distributed file system, in which a UNC/Mapped share is NOT tied to any specific drive, so there youd have to actually sum up several drives.

    And what about user quotas? The drive may not be full, but the account you are using to write to that folder may have hit its limit.

    0 讨论(0)
提交回复
热议问题