【问题标题】:.Net Core Find Free Disk Space on Different OS.Net Core 在不同操作系统上查找可用磁盘空间
【发布时间】:2021-02-06 07:08:17
【问题描述】:

有没有办法使用 C# ASP.NET Core 在不同的操作系统(主要是 Linux 和 Windows)上找到可用空间。

我找到了一种方法(使用 DriveInfo),通过将驱动器名称作为参数传递来获取可用空间。这在 Windows 上运行良好,但我也希望在 Linux 上也一样。

public static int CheckDiskSpace(string driveLetter)
{
    DriveInfo drive = new DriveInfo(driveLetter);

    var totalBytes = drive.TotalSize;
    var freeBytes = drive.AvailableFreeSpace;

    var freePercent = (int)((100 * freeBytes) / totalBytes);

    return freePercent;
}

传递驱动器(C:/)如下:

var freespace = DriveDetails.CheckDiskSpace("C:/");

更新:这也适用于 Linux。

【问题讨论】:

  • 您知道您的代码作为 ASP.NET Core 应用程序的一部分运行在服务器上而不是客户端上,对吗?所以你总是会得到大致相同的信息,也就是服务器的磁盘空间
  • 我的问题标题被修改了,我需要在不同的操作系统上找到应用程序的可用磁盘空间
  • @johnny5 该副本甚至与这里所问的内容都不接近。 OP 想要当前运行的应用程序的路径,而不是源代码的路径。编辑不太合适
  • 我的错误,但当前标题误导了这个问题与查找路径而不是磁盘空间有关
  • @johnny5,查找路径是问题的一部分,主要问题是查找磁盘空间。

标签: c# asp.net-core .net-core


【解决方案1】:

对于 Linux 下的 Net.Core,您可以调用

var freeBytes = new DriveInfo(path).AvailableFreeSpace; 

其中 path 是一些相对或绝对文件夹名称,它会自动为您提供有关存储此路径的分区的驱动器信息。在 Net.Core 2.2 上测试。

相比之下,在 Windows 中,您可以:

A) 需要提供盘符(很遗憾不能直接从相对路径推导出来,所以你需要做一些额外的工作,而且根本无法为 UNC 路径计算)。

B) 需要使用 Windows API(这也适用于 UNC 路径):

[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);

GetDiskFreeSpaceEx(path, out var freeBytes, out var _, out var __);

还有一些其他例外情况,所以最后我的用法如下:

#if DEBUG        
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
    out long lpFreeBytesAvailable,
    out long lpTotalNumberOfBytes,
    out long lpTotalNumberOfFreeBytes);
#endif

public long? CheckDiskSpace()
{
    long? freeBytes = null;

    try     
    {
#if DEBUG //RuntimeInformation and OSPlatform seem to not exist while building for Linux platform
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
            long freeBytesOut;
            //On some drives (for example, RAM drives, GetDiskFreeSpaceEx does not work
            if (GetDiskFreeSpaceEx(_path, out freeBytesOut, out var _, out var __))
                freeBytes = freeBytesOut;
        }
#endif

        if (freeBytes == null)
        {
            //DriveInfo works well on paths in Linux    //TODO: what about Mac?
            var drive = new DriveInfo(_path);
            freeBytes = drive.AvailableFreeSpace;
        }
    }
    catch (ArgumentException)
    {
        //ignore the exception
    }

    return freeBytes;
}

【讨论】:

    【解决方案2】:

    如果你使用.Net Core,你可以使用System.AppContext.BaseDirectory

    (或)

    Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-21
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多