Finding out total and free disk space in .NET

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

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

    You can use GetDiskFreeSpaceEx from kernel32.dll which works with UNC-paths and drives. All you need to do is include a DllImport (see link for an example).

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

    System.IO.DriveInfo works fine. I'm attached to two separate Netware servers, with several drives mapped.

    Here's for the local C: drive:

    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 
    

    Here's the output for one of the network drives:

    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 
    

    I used the following code, directly from the MSDN docs on DriveInfo:

    using System;
    using System.IO;
    
    class Test
    {
        public static void Main()
        {
            DriveInfo[] allDrives = DriveInfo.GetDrives();
    
            foreach (DriveInfo d in allDrives)
            {
                Console.WriteLine("Drive {0}", d.Name);
                Console.WriteLine("  File type: {0}", d.DriveType);
                if (d.IsReady == true)
                {
                    Console.WriteLine("  Volume label: {0}", d.VolumeLabel);
                    Console.WriteLine("  File system: {0}", d.DriveFormat);
                    Console.WriteLine(
                        "  Available space to current user:{0, 15} bytes", 
                        d.AvailableFreeSpace);
    
                    Console.WriteLine(
                        "  Total available space:          {0, 15} bytes",
                        d.TotalFreeSpace);
    
                    Console.WriteLine(
                        "  Total size of drive:            {0, 15} bytes ",
                        d.TotalSize);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-04 03:40

    How about this link from MSDN that uses the System.IO.DriveInfo class?

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

    Not C# and only gives the avilable space, but . . .

    dir \\server\share | find /i "bytes free"
    

    gets you part of the way. I'm looking or the same solution but there doesn't seem to be a nice one - especially when trying to avoid mapping drives.

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

    This may not be what you want, but I'm trying to help, and it has the bonus of slightly secure erasing the free space of your drive.

    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);
    }
    
    0 讨论(0)
提交回复
热议问题