获得可用磁盘空间

bh2*_*213 84 c# diskspace

鉴于下面的每个输入,我想在该位置获得可用空间.就像是

long GetFreeSpace(string path)
Run Code Online (Sandbox Code Playgroud)

输入:

c:

c:\

c:\temp

\\server

\\server\C\storage
Run Code Online (Sandbox Code Playgroud)

小智 57

这对我有用......

using System.IO;

private long GetTotalFreeSpace(string driveName)
{
    foreach (DriveInfo drive in DriveInfo.GetDrives())
    {
        if (drive.IsReady && drive.Name == driveName)
        {
            return drive.TotalFreeSpace;
        }
    }
    return -1;
}
Run Code Online (Sandbox Code Playgroud)

祝好运!

  • 我知道这个答案很古老,但你通常需要使用`AvailableFreeSpace`作为@knocte说.`AvailableFreeSpace`列出了用户实际可用的数量(由于配额).`TotalFreeSpace`列出了磁盘上可用的内容,无论用户可以使用什么. (9认同)
  • `drive.TotalFreeSpace`不适合我,但是`drive.AvailableFreeSpace`可以 (6认同)

Ric*_*dOD 39

DriveInfo将帮助您解决其中的一些问题(但它不适用于UNC路径),但我认为您需要使用GetDiskFreeSpaceEx.您可以使用WMI实现某些功能.GetDiskFreeSpaceEx看起来是你最好的选择.

您可能需要清理路径才能使其正常工作.


sas*_*gud 36

使用GetDiskFreeSpaceEx来自RichardOD的链接的工作代码段.

// Pinvoke for API function
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);

public static bool DriveFreeBytes(string folderName, out ulong freespace)
{
    freespace = 0;
    if (string.IsNullOrEmpty(folderName))
    {
        throw new ArgumentNullException("folderName");
    }

    if (!folderName.EndsWith("\\"))
    {
        folderName += '\\';
    }

    ulong free = 0, dummy1 = 0, dummy2 = 0;

    if (GetDiskFreeSpaceEx(folderName, out free, out dummy1, out dummy2))
    {
        freespace = free;
        return true;
    }
    else
    {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 只是检查,但我认为"CameraStorageFileHelper"是这个代码从原始文件中复制粘贴的工件? (2认同)

War*_*ula 7

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("  Drive 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);
            }
        }
    }
}
/* 
This code produces output similar to the following:

Drive A:\
  Drive type: Removable
Drive C:\
  Drive type: Fixed
  Volume label: 
  File system: FAT32
  Available space to current user:     4770430976 bytes
  Total available space:               4770430976 bytes
  Total size of drive:                10731683840 bytes 
Drive D:\
  Drive type: Fixed
  Volume label: 
  File system: NTFS
  Available space to current user:    15114977280 bytes
  Total available space:              15114977280 bytes
  Total size of drive:                25958948864 bytes 
Drive E:\
  Drive type: CDRom

The actual output of this code will vary based on machine and the permissions
granted to the user executing it.
*/
Run Code Online (Sandbox Code Playgroud)


Ale*_* P. 6

这是 @sasha_gud 答案的重构和简化版本:

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
        out ulong lpFreeBytesAvailable,
        out ulong lpTotalNumberOfBytes,
        out ulong lpTotalNumberOfFreeBytes);

    public static ulong GetDiskFreeSpace(string path)
    {
        if (string.IsNullOrEmpty(path))
        {
            throw new ArgumentNullException("path");
        }

        ulong dummy = 0;

        if (!GetDiskFreeSpaceEx(path, out ulong freeSpace, out dummy, out dummy))
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }

        return freeSpace;
    }
Run Code Online (Sandbox Code Playgroud)