Finding out total and free disk space in .NET

前端 未结 9 1232
我在风中等你
我在风中等你 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:26

    Not really a C# example but may give you a hint - a VB.NET function returning both amount of free and total space on drive (in bytes) along specified path. Works for UNC paths as well, unlike System.IO.DriveInfo.

    VB.NET:

     _
    Private Shared Function GetDiskFreeSpaceEx(lpDirectoryName As String, ByRef lpFreeBytesAvailable As ULong, ByRef lpTotalNumberOfBytes As ULong, ByRef lpTotalNumberOfFreeBytes As ULong) As  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
    

提交回复
热议问题