不使用 DriveInfo.GetDrives 列出除 DVD 驱动器之外的所有磁盘

List all disks except DVD drive without using DriveInfo.GetDrives

如何在不使用 DriveInfo.GetDrives 的情况下列出除 DVD 驱动器 之外的所有磁盘?我正在使用 Unity 游戏引擎,它在 GetDrives 上抛出错误:

NotImplementedException: The requested feature is not implemented. System.IO.DriveInfo.WindowsGetDrives

我正在寻找解决方法。我读到 Winbase.h 有一个 DriveType 枚举,有什么方法可以使用它吗?

你不能,不能不牺牲 platform-independence。

以下将获得所有个驱动器。我不认为有一种 platform-independent 方法可以得到 "all drives except DVD":

string[] drives = Directory.GetLogicalDrives();

您提到了 Winbase.h。那是 C++,会将您绑定到特定平台 (Windows)。您 可以 使用 p/invoke 执行此操作,但您必须编写 platform-dependent 代码。我不会向初学者推荐 P/invoke。这是一个高级主题,很快就会变得困难。

"platform-dependent"是什么意思?您必须为 Windows、Linux、Mac 以及您的代码将在 运行 上运行的任何其他平台编写代码。 Unity 的卖点之一是您可以 编写一次代码 并期望它在各种不同的平台上 运行 相同。

感谢 Amy 的回答,我通过 C.R 找到了 this post。 Timmons Consulting, Inc. 已经过测试并致力于 Windows,如果 Mac 上的人可以检查它是否也有效,我将不胜感激:

public enum DriveType : int
{
Unknown = 0,
NoRoot = 1,
Removable = 2,
Localdisk = 3,
Network = 4,
CD = 5,
RAMDrive = 6
}

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetDriveType(string lpRootPathName);

用法示例:

using System;
using System.Runtime.InteropServices;

void GetDrives()
{
     foreach (string s in Environment.GetLogicalDrives())
     Console.WriteLine(string.Format("Drive {0} is a {1}.",
     s, Enum.GetName(typeof(DriveType), GetDriveType(s))));
}