Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “什么样的驱动力?”;“无目录”;(System.IO.DriveType.NoRootDirectory)?_C#_Drive - Fatal编程技术网

C# “什么样的驱动力?”;“无目录”;(System.IO.DriveType.NoRootDirectory)?

C# “什么样的驱动力?”;“无目录”;(System.IO.DriveType.NoRootDirectory)?,c#,drive,C#,Drive,在C#系统中,IO.DriveInfo具有属性DriveType System.IO.DriveType是一个枚举: public enum DriveType { Unknown = 0, // // Summary: // The drive does not have a root directory. NoRootDirectory = 1, Removable = 2, Fixed = 3, Network = 4

在C#
系统中,IO.DriveInfo
具有属性
DriveType

System.IO.DriveType
是一个枚举:

public enum DriveType
{
    Unknown = 0,
    //
    // Summary:
    //     The drive does not have a root directory.
    NoRootDirectory = 1,
    Removable = 2,
    Fixed = 3,
    Network = 4,
    CDRom = 5,
    Ram = 6,
}
我怀疑这是一个没有驱动器号的卷。但使用:

System.IO.DriveInfo.GetDrives();
没有列出没有驱动器号的卷


NoRootDirectory
用于任何其他类型的卷/驱动器,还是
System.IO.DriveInfo.GetDrives()
只是不显示它们?
System.IO.DriveType.NoRootDirectory
似乎是对“此驱动器号未使用”的误导性指定

所有驱动器的测试代码:所有未找到的驱动器都具有类型DriveType.NoRootDirectory

foreach (char driveLetter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToArray())
{
    var driveInfo = new System.IO.DriveInfo(driveLetter.ToString() + ":\\");

    if(System.IO.DriveInfo.GetDrives().FirstOrDefault(o => o.Name[0] == driveLetter) == null)
        Console.WriteLine("// Not found: " + driveInfo.Name + " has DriveType: " + driveInfo.DriveType.ToString());
    else
        Console.WriteLine("//     found: " + driveInfo.Name + " has DriveType: " + driveInfo.DriveType.ToString());
}
结果:

// Not found: A:\ has DriveType: NoRootDirectory
// Not found: B:\ has DriveType: NoRootDirectory
//     found: C:\ has DriveType: Fixed
//     found: D:\ has DriveType: CDRom
// Not found: E:\ has DriveType: NoRootDirectory
// Not found: F:\ has DriveType: NoRootDirectory
// Not found: G:\ has DriveType: NoRootDirectory
// Not found: H:\ has DriveType: NoRootDirectory
// Not found: I:\ has DriveType: NoRootDirectory
// Not found: J:\ has DriveType: NoRootDirectory
// Not found: K:\ has DriveType: NoRootDirectory
// Not found: L:\ has DriveType: NoRootDirectory
// Not found: M:\ has DriveType: NoRootDirectory
// Not found: N:\ has DriveType: NoRootDirectory
// Not found: O:\ has DriveType: NoRootDirectory
//     found: P:\ has DriveType: Network
// Not found: Q:\ has DriveType: NoRootDirectory
//     found: R:\ has DriveType: Network
//     found: S:\ has DriveType: Network
// Not found: T:\ has DriveType: NoRootDirectory
// Not found: U:\ has DriveType: NoRootDirectory
//     found: V:\ has DriveType: Network
//     found: W:\ has DriveType: Fixed
//     found: X:\ has DriveType: Network
//     found: Y:\ has DriveType: Network
//     found: Z:\ has DriveType: Network

我知道它用于未分配的驱动器号。当然,您不会通过
GetDrives
获得它们,但可以尝试
newsystem.IO.DriveInfo(“B:”).DriveType
或类似方法。它也可以用于未格式化分区(或未知文件系统),但我不能完全确定(在这种情况下,您必须测试是获得
unknown
还是
NoRootDirectory
)。为完整起见,您还可以通过转到
HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\DOS设备来创建垃圾驱动器,并创建一个指向
\Device\Null
的驱动器
X:
,例如,查看这些驱动器的功能

事实上,答案更清楚一些。它说:

根路径无效;例如,指定路径上没有装入卷

我将“根路径无效”翻译为“内核路径
\DosDevices\X:
无法解析/链接到能够解析路径请求的有效文件系统目录对象
\

可能这句话是由具有Windows内核知识的人写的。在这种情况下,我假设我上面的“垃圾驱动器”也会给你这个值,以及任何未分配的驱动器号


要更详细地阅读我刚才提到的内容,请查看您是否感兴趣。

@HansPassant这是我测试的内容。查看编辑。当面对装载到NTFS文件夹中的分区时,这是如何表现的?欢迎使用堆栈溢出!感谢您提供这段代码片段,它可能会提供一些即时帮助。通过说明为什么这是一个很好的解决问题的方法,正确地解释它的教育价值,并将使它对未来有类似但不完全相同问题的读者更有用。请在回答中添加解释,并说明适用的限制和假设。
//Check fixed local drive
string path= "C\MyFolder\myfile.txt";
string root = Path.GetPathRoot(path);

if (DriveInfo.GetDrives().FirstOrDefault(d => d.Name == root).DriveType == DriveType.Fixed)
{
    MessageBox.Show(root);
}