none
get free space on network share RRS feed

  • Question

  • I'm trying to figure out a way to get the available free space on a network share without mounting it as a drive letter. So for example get the remaining space for \\someserver\share1 .
    With local drive letters I'd do it via System.IO.DriveInfo but alas, it only works for drive letters.

    Maybe it works via WMI but the last time I used it it was like ages ago.

    Thursday, January 28, 2010 9:28 AM

Answers

  • using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime.InteropServices;
    using System.Security;
    
    namespace ConsoleApplication1
    {
        static class Program
        {
            static void Main()
            {
                string networkFolder = @"\\NetworkShare\SomeValidFolder";
                Console.WriteLine("Space = " + FreeSpace(networkFolder));
            }
    
            /// <summary>
            /// Returns free space for drive containing the specified folder, or returns -1 on failure.
            /// </summary>
            /// <param name="folderName">A folder on the drive in question.</param>
            /// <returns>Space free on the volume containing 'folder_name' or -1 on error.</returns>
    
            public static long FreeSpace(string folderName)
            {
                if (string.IsNullOrEmpty(folderName))
                {
                    throw new ArgumentNullException("folderName");
                }
    
                if (!folderName.EndsWith("\\"))
                {
                    folderName += '\\';
                }
    
                long free=0, dummy1=0, dummy2=0;
    
                if (GetDiskFreeSpaceEx(folderName, ref free, ref dummy1, ref dummy2))
                {
                    return free;
                }
                else
                {
                    return -1;
                }
            }
    
            [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage"), SuppressUnmanagedCodeSecurity]
            [DllImport("Kernel32", SetLastError=true, CharSet=CharSet.Auto)]
            [return: MarshalAs(UnmanagedType.Bool)]
    
            private static extern bool GetDiskFreeSpaceEx
            (
                string lpszPath,                    // Must name a folder, must end with '\'.
                ref long lpFreeBytesAvailable,
                ref long lpTotalNumberOfBytes,
                ref long lpTotalNumberOfFreeBytes
            );
        }
    }
    
    • Marked as answer by Frank M Monday, February 1, 2010 8:52 AM
    Thursday, January 28, 2010 11:19 AM

All replies

  • using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime.InteropServices;
    using System.Security;
    
    namespace ConsoleApplication1
    {
        static class Program
        {
            static void Main()
            {
                string networkFolder = @"\\NetworkShare\SomeValidFolder";
                Console.WriteLine("Space = " + FreeSpace(networkFolder));
            }
    
            /// <summary>
            /// Returns free space for drive containing the specified folder, or returns -1 on failure.
            /// </summary>
            /// <param name="folderName">A folder on the drive in question.</param>
            /// <returns>Space free on the volume containing 'folder_name' or -1 on error.</returns>
    
            public static long FreeSpace(string folderName)
            {
                if (string.IsNullOrEmpty(folderName))
                {
                    throw new ArgumentNullException("folderName");
                }
    
                if (!folderName.EndsWith("\\"))
                {
                    folderName += '\\';
                }
    
                long free=0, dummy1=0, dummy2=0;
    
                if (GetDiskFreeSpaceEx(folderName, ref free, ref dummy1, ref dummy2))
                {
                    return free;
                }
                else
                {
                    return -1;
                }
            }
    
            [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage"), SuppressUnmanagedCodeSecurity]
            [DllImport("Kernel32", SetLastError=true, CharSet=CharSet.Auto)]
            [return: MarshalAs(UnmanagedType.Bool)]
    
            private static extern bool GetDiskFreeSpaceEx
            (
                string lpszPath,                    // Must name a folder, must end with '\'.
                ref long lpFreeBytesAvailable,
                ref long lpTotalNumberOfBytes,
                ref long lpTotalNumberOfFreeBytes
            );
        }
    }
    
    • Marked as answer by Frank M Monday, February 1, 2010 8:52 AM
    Thursday, January 28, 2010 11:19 AM
  • I've tried both solutions and the WMI Way in the other thread gave me some weird access denied error. I don't have that much time to fiddle around with it right now but since I like anything non-interop more than interop somehow I'll get back to it later. ;)

    Right now I'll use the interop Way which works like a charm.

    Thanks alot!

    Monday, February 1, 2010 8:52 AM