2016-05-19 14 views
7

In C# System.IO.DriveInfo ha la proprietà DriveType.Che tipo di unità è "NoRootDirectory" (System.IO.DriveType.NoRootDirectory)?

System.IO.DriveType è un enum:

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, 
} 

ho il sospetto che questo è un volume senza una lettera di unità. Ma usando:

System.IO.DriveInfo.GetDrives(); 

non elenca il mio volume senza lettera di unità.

È NoRootDirectory utilizzato per qualsiasi altro tipo di volumi/unità o non lo fa semplicemente System.IO.DriveInfo.GetDrives()?

+0

So che è utilizzato per le lettere di unità non assegnate. Ovviamente non li otterrete tramite 'GetDrives' ma proverai' nuovo System.IO.DriveInfo ("B:"). DriveType' o simile. Potrebbe essere usato anche per le partizioni non formattate (o per i filesystem sconosciuti), ma non ne sono del tutto sicuro (dovresti verificare se in questo caso ottieni "Unknown" o "NoRootDirectory"). Per completezza, potresti anche creare unità di spazzatura andando su 'HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ DOS Devices 'e creando un disco' X: 'puntando a' \ Device \ Null' per esempio e vedi cosa ottieni per coloro. – CherryDT

+0

In realtà, la [documentazione per la funzione WinAPI sottostante 'GetDriveType'] (https://msdn.microsoft.com/en-us/library/windows/desktop/aa364939 (v = vs.85) .aspx) è un po ' più chiaro. Dice: "Il percorso root non è valido, ad esempio non è stato montato alcun volume nel percorso specificato." – CherryDT

+0

Tradurrei "il percorso root non valido" come "Il percorso kernel' \ DosDevices \ X: 'non risolve/collega a un oggetto directory di filesystem valido in grado di risolvere una richiesta per il percorso' \ '." - Probabilmente la frase è stata scritta da qualcuno con conoscenza del kernel di Windows. In questo caso, suppongo che le mie "unità di spazzatura" da sopra possano anche darti questo valore, così come ogni lettera di unità non assegnata. – CherryDT

risposta

4

System.IO.DriveType.NoRootDirectory sembra essere una designazione fuorviante per "Questa lettera di unità è inutilizzata"

Testcode per tutte le unità: Tutto non si trovano le unità hanno il tipo 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()); 
} 

Risultato:

// 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 

Ex esempio (prima di modificare questo post):

Ma: Questa è solo una trovata caso in cui NoRootDirectory viene utilizzato. Non risponde alla domanda se ci siano altre costellazioni in cui verrà mostrato.

Qui il codice di prova:

// 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 

Ho testato alcuni più combinazioni con utilizzato, ma non è presente unità, ma nessuno di loro è tornato NoRootDirectory.

+0

@HansPassant Questo è quello che ho provato. Guarda la modifica. – boboes

+0

Come si comporta questo quando si affronta una partizione montata in una cartella in NTFS? –

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); 
} 
+4

Benvenuto in Stack Overflow! Grazie per questo snippet di codice, che potrebbe fornire un aiuto immediato. Una spiegazione appropriata [migliorerebbe notevolmente] (// meta.stackexchange.com/q/114762) il suo valore educativo mostrando * perché * questa è una buona soluzione al problema e renderebbe più utile ai futuri lettori con simili, ma non identiche, domande. Si prega di [modificare] la risposta per aggiungere una spiegazione e fornire un'indicazione di quali limitazioni e ipotesi si applicano. –

Problemi correlati