2009-03-24 18 views
3

Guten Morgen,.Net DriveInfo() mit UNC-Pfaden?

ist es eine Möglichkeit, eine Instanz für Driveinfo UNC-Pfade (zB "\ fors343a.ww123.somedomain.net \ Ordner \ 1 \") zu erhalten, weil zum Beispiel ...

var driveInfo = new System.IO.DriveInfo(drive); 

... löst eine ArgumentException ("Objekt muss ein Stammverzeichnis (\" C: \\ ") oder ein Laufwerksbuchstabe (\" C \ ") sein.") Wenn Sie diesen UNC-Pfad oben verwenden.

Was würde ich verwenden, um Informationen darüber oder z. Wie würde ich überprüfen, ob sich ein bestimmter Ordner auf einem lokalen Laufwerk oder auf einem unc-Pfad befindet?

Antwort

3

Der Abschnitt Hinweise für die DriveInfo Konstruktor sagt:

Der Laufwerksname entweder ein Groß- oder Kleinbuchstaben von ‚a‘ bis ‚z‘ sein muss. Sie können diese Methode nicht verwenden, um Informationen zu Laufwerksnamen zu erhalten, die nullNothingnullptra Nullverweis (Nothing in Visual Basic) oder UNC (\ server \ share) Pfade sind.

Ich konnte es durch Zuordnen eines Netzlaufwerks in Windows Explorer arbeiten lassen. Das heißt, ich habe "\ server \ share" auf Laufwerk Z abgebildet und dann DriveInfo("Z:\\"); gab mir, was ich erwartet hatte.

Leider ist es nicht einfach, ein Netzlaufwerk von C# zuzuordnen. Sie müssen entweder einen externen Befehl ausführen (z. B. "net use z: \ server \ share") oder die Windows-API-Funktion WNetAddConnection2 aufrufen, um dies zu tun. So oder so, Sie müssen die Laufwerkszuordnung entfernen, wenn Sie fertig sind.

0

Unter Windows funktioniert die folgende große in C# (zumindest die Größen zu bekommen, was die meisten ist häufig erforderlich):

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

Hier ist ein Beispielcode (dies tatsächlich nicht kompiliert wurde in dieser Form, ist aber Kleinkram aus funktionierenden Code in mehreren Dateien verstreut):

/// <summary> 
/// A compilation of the properties of folders and files in a file system. 
/// </summary> 
public struct FileSystemProperties 
{ 
    private FileSystemProperties(long? totalBytes, long? freeBytes, long? availableBytes) 
     : this() 
    { 
     TotalBytes = totalBytes; 
     FreeBytes = freeBytes; 
     AvailableBytes = availableBytes; 
    } 
    /// <summary> 
    /// Gets the total number of bytes on the drive. 
    /// </summary> 
    public long? TotalBytes { get; private set; } 
    /// <summary> 
    /// Gets the number of bytes free on the drive. 
    /// </summary> 
    public long? FreeBytes { get; private set; } 
    /// <summary> 
    /// Gets the number of bytes available on the drive (counts disk quotas). 
    /// </summary> 
    public long? AvailableBytes { get; private set; } 

    /// <summary> 
    /// Gets the properties for this file system. 
    /// </summary> 
    /// <param name="volumeIdentifier">The path whose volume properties are to be queried.</param> 
    /// <param name="cancel">An optional <see cref="CancellationToken"/> that can be used to cancel the operation.</param> 
    /// <returns>A <see cref="FileSystemProperties"/> containing the properties for the specified file system.</returns> 
    public static FileSystemProperties GetProperties(string volumeIdentifier) 
    { 
     ulong available; 
     ulong total; 
     ulong free; 
     if (GetDiskFreeSpaceEx(volumeIdentifier, out available, out total, out free)) 
     { 
      return new FileSystemProperties((long)total, (long)free, (long)available); 
     } 
     return new FileSystemProperties(null, null, null); 
    } 
    /// <summary> 
    /// Asynchronously gets the properties for this file system. 
    /// </summary> 
    /// <param name="volumeIdentifier">The path whose volume properties are to be queried.</param> 
    /// <param name="cancel">An optional <see cref="CancellationToken"/> that can be used to cancel the operation.</param> 
    /// <returns>A <see cref="Task"/> containing the <see cref="FileSystemProperties"/> for this entry.</returns> 
    public static async Task<FileSystemProperties> GetPropertiesAsync(string volumeIdentifier, CancellationToken cancel = default(CancellationToken)) 
    { 
     return await Task.Run(() => 
     { 
      ulong available; 
      ulong total; 
      ulong free; 
      if (GetDiskFreeSpaceEx(volumeIdentifier, out available, out total, out free)) 
      { 
       return new FileSystemProperties((long)total, (long)free, (long)available); 
      } 
      return new FileSystemProperties(null, null, null); 
     }, cancel); 
    } 
} 

versuchen sie nicht, dieses auf Linux oder Mac zu verwenden - es wäre für diejenigen werden müssen neu geschrieben (und ich Ich bin daran interessiert, das zu sehen).