.Net DriveInfo () with UNC paths?

Good morning,

Is there a way to get a DriveInfo instance for UNC paths (for example, "\ fors343a.ww123.somedomain.net \ folder \ 1 \") because, for example ...

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

... throws an ArgumentException ("The object must be the root directory (" C: \\ ") or the drive letter (" C \ ").") when using this UNC path above.

What would I use to get information about this or, for example, how can I check if a given folder is on a local drive or path unc?

+3
unc
Mar 24 '09 at 9:56
source share
2 answers

The Remarks section for the DriveInfo constructor indicates:

The drive name must be either an uppercase or lowercase letter from 'a' to 'z'. You cannot use this method to get information about drive names that are null zeros of Nothingnullptra (Nothing in Visual Basic) or use UNC (\ server \ share).

I managed to get it working by mapping a network drive in Windows Explorer. That is, I displayed "\ server \ share" to control Z, and then DriveInfo("Z:\\"); gave me what i expected.

Unfortunately, there is no easy way to map a network drive to C #. You will have to either execute an external command (for example, "net use z: \ server \ share") or call the Windows WNetAddConnection2 API to do this. In any case, you will need to remove the disk card when you are done.

+3
Mar 24 '09 at 22:37
source share

On Windows, in C # it works fine (at least to get the dimensions, which is most often necessary):

  [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); 

Here is a sample code (in fact, it was not compiled in this form, but these are bits and fragments from working code scattered across several files):

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

Do not try to use this on Linux or Mac - you will need to rewrite it for them (and I would be interested to see it).

0
Jan 01
source share



All Articles