2009-03-24 1 views
3

좋은 아침, .Net DriveInfo()에 UNC 경로가 있습니까?

는 UNC 경로에 대한 DriveInfo를 인스턴스를 얻을 수있는 방법이있다 (예를 들어, "\ fors343a.ww123.somedomain.net 폴더 \ 1 \ \") ... 때문에 예를 들어

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

... 위의 해당 UNC 경로를 사용할 때 ArgumentException이 발생합니다 ("루트 디렉토리 (\"C : \\ ") 또는 드라이브 문자 (\"C \ ") 여야합니다.").

정보를 검색하기 위해 무엇을 사용합니까? 예 : 주어진 폴더가 로컬 드라이브 또는 unc 경로에 있는지 여부를 어떻게 확인할 수 있습니까?

답변

3

DriveInfo 생성자에 대한 설명 부분은 말한다 :

드라이브 이름은 'Z'에 'A' 에서 대문자와 소문자이어야합니다. 이 메서드를 사용하여 nullNothingnullptra null 참조 (Visual Basic의 경우 Nothing) 또는 UNC (\ server \ share) 경로를 사용하는 드라이브 이름에 대한 정보를 얻을 수 없습니다.

Windows 탐색기에서 네트워크 드라이브를 매핑하여 작동시킬 수있었습니다. 즉, "\ server \ share"를 Z 드라이브에 매핑 한 다음 DriveInfo("Z:\\");은 내가 예상 한 것을 제공했습니다.

불행히도 C#에서 네트워크 드라이브를 매핑하는 간단한 방법은 없습니다. 외부 명령 (예 : "net use z : \ server \ share")을 실행하거나 Windows WNetAddConnection2 API 함수를 호출하여이를 수행해야합니다. 어느쪽으로 든 가면 드라이브 매핑을 제거해야합니다. 이 사실은 컴파일되지 않은 (여기서

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

이 몇 가지 예제 코드입니다 :

Windows에서
0

, 다음은 (적어도 크기, 가장 일반적으로 필요한 것입니다 얻을) C#에서 좋은 작품 이 양식에 여러 파일에 분산 작업 코드에서 비트와 조각)이지만 :

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

리눅스 또는 Mac에서이를 사용하지 마십시오 - 그것은 사람들을 위해 다시 작성되어야 할 것이다 (그리고 나 ' 관심을 보일 것).

관련 문제