什么样的驱动器是“NoRootDirectory”(System.IO.DriveType.NoRootDirectory)?

时间:2016-05-19 11:11:14

标签: c# drive

在C#System.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()只是不显示它们?

3 个答案:

答案 0 :(得分:4)

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

以前的例子(在编辑这篇文章之前):

  

但是:这只是一个使用NoRootDirectory的案例。它没有回答是否存在其他星座的问题。

这里是测试代码:

// J:\ is an unused drive letter
var xDummy1 = new System.IO.DriveInfo("J:\\");
Console.WriteLine(xDummy1.Name);                        // result: J:\
Console.WriteLine(xDummy1.DriveType);                   // result: NoRootDirectory 
Console.WriteLine(xDummy1.IsReady);                     // result: False 
// Console.WriteLine(xDummy1.DriveFormat); // would throw an DriveNotFoundException
Console.WriteLine(System.IO.Directory.Exists("j:\\"));  // result: False


// S:\ is an mapped drive to a currently not available share
var xDummy2 = new System.IO.DriveInfo("S:\\");          
Console.WriteLine(xDummy2.Name);                        // result: S:\ 
Console.WriteLine(xDummy2.DriveType);                   // result: Network
Console.WriteLine(xDummy2.IsReady);                     // result: False 
// Console.WriteLine(xDummy2.DriveFormat); // would throw an IOException
Console.WriteLine(System.IO.Directory.Exists("S:\\"));  // result: False

我已经测试了一些使用但不存在的驱动器的更多组合,但没有一个返回NoRootDirectory

答案 1 :(得分:1)

我知道它用于未分配的驱动器号。当然,您不会让他们通过GetDrives,而是尝试new System.IO.DriveInfo("B:").DriveType之类的方法。它也可以用于未格式化的分区(或未知的文件系统),但是我不确定(在这种情况下,您必须测试是否得到UnknownNoRootDirectory)。为了完整起见,您还可以通过转到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices并创建指向X:的驱动器\Device\Null来创建垃圾驱动器,然后查看获得的结果。

实际上,documentation for the underlying WinAPI function GetDriveType更加清晰。它说:

根路径无效;例如,在指定的路径上没有安装卷。

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

该句子可能是由具有Windows内核知识的人写的。在这种情况下,我假设上面的“垃圾驱动器”也会为您提供该值以及所有未分配的驱动器号。

要详细了解我刚才提到的内容,如果有兴趣,请查看http://www.osronline.com/article.cfm%5eid=107.htm

答案 2 :(得分:0)

//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);
}