CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2008
    Posts
    2

    How to find available space on a network share folder??

    Does anybody know how to get available space on a network share folder programmatically in C#?

    So far the solutions that I have seen, require mapping share folders as a drive. Is there any way possible without mapping it?

    Thanks in advance.

  2. #2
    Join Date
    Sep 2004
    Posts
    65

    Re: How to find available space on a network share folder??

    I have done this before by using an API call to GetDiskFreeSpaceEx. Here's an example. It's in VB.NET, but it should be pretty easy to convert to C#...

    Code:
    'This class contains the functionality and information relating to a drive.
    Public Class DriveSpace
    
        'API function to obtain drive information
        Private Declare Auto Function GetDiskFreeSpaceEx Lib "kernel32.dll" ( _
            ByVal Drive As String, _
            ByRef FreeBytesAvailableToCaller As System.UInt64, _
            ByRef TotalNumberOfBytes As System.UInt64, _
            ByRef TotalNumberOfFreeBytes As System.UInt64) As Integer
    
        Dim mDriveName As String
        Dim mFreeSpace As Long
        Dim mUsedSpace As Long
        Dim mFreePercent As Double
        Dim mUsedPercent As Double
        Dim mTotalSpace As Long
    
        'Create a new DriveSpace by passing in the drive name
        Public Sub New(ByVal driveName As String)
    
            Dim uavailable As System.UInt64
            Dim utotal As System.UInt64
            Dim ufree As System.UInt64
    
            GetDiskFreeSpaceEx(driveName, uavailable, utotal, ufree)
    
            mDriveName = driveName
            mFreeSpace = Convert.ToInt64(uavailable) / 1024 / 1024
            mTotalSpace = Convert.ToInt64(utotal) / 1024 / 1024
            mUsedSpace = (mTotalSpace - mFreeSpace)
            mFreePercent = (mFreeSpace / mTotalSpace)
            mUsedPercent = (mUsedSpace / mTotalSpace)
        End Sub
    
        Public Property DriveName() As String
            Get
                Return mDriveName
            End Get
            Set(ByVal value As String)
                mDriveName = value
            End Set
        End Property
    
        Public ReadOnly Property FreeSpace() As Long
            Get
                Return mFreeSpace
            End Get
        End Property
    
        Public ReadOnly Property UsedSpace() As Long
            Get
                Return mUsedSpace
            End Get
        End Property
    
        Public ReadOnly Property TotalSpace() As Long
            Get
                Return mTotalSpace
            End Get
        End Property
    
        Public ReadOnly Property FreePercent() As Double
            Get
                Return mFreePercent
            End Get
        End Property
    
        Public ReadOnly Property UsedPercent() As Double
            Get
                Return mUsedPercent
            End Get
        End Property
    
    End Class
    -Ranthalion

  3. #3
    Join Date
    Feb 2008
    Posts
    2

    Re: How to find available space on a network share folder??

    Thanks Ranthalion.

    But GetDiskFreeSpaceEx method requires a drive name. It does not take a shared path.

    My input is something like

    \\GlobalShare\Media\Audio\

    I want to get free space at this path. i.e. number of bytes that are available for use.


    Thanks.

  4. #4
    Join Date
    Sep 2004
    Posts
    65

    Re: How to find available space on a network share folder??

    You should be able to send it the path to a shared drive. I am using it in an application that monitors network shares and it is working for paths like \\BillingPC\InvoiceShare$\. Have you tried the code?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured