.Net DriveInfo()是否具有UNC路径?

.Net DriveInfo()是否具有UNC路径?,.net,unc,driveinfo,.net,Unc,Driveinfo,早上好 是否有方法获取UNC路径的DriveInfo实例(例如“\fors343a.ww123.somedomain.net\folder\1\”),因为例如 var driveInfo = new System.IO.DriveInfo(drive); 。。。使用上面的UNC路径时引发ArgumentException(“对象必须是根目录(\“C:\”)或驱动器号(\“C\”) 我将使用什么来检索有关该文件夹的信息,例如,我将如何检查给定文件夹是否位于本地驱动器或unc路径上?DriveIn

早上好

是否有方法获取UNC路径的DriveInfo实例(例如“\fors343a.ww123.somedomain.net\folder\1\”),因为例如

var driveInfo = new System.IO.DriveInfo(drive);
。。。使用上面的UNC路径时引发ArgumentException(“对象必须是根目录(\“C:\”)或驱动器号(\“C\”)


我将使用什么来检索有关该文件夹的信息,例如,我将如何检查给定文件夹是否位于本地驱动器或unc路径上?

DriveInfo构造函数的备注部分说:

驱动器名必须是 来自“a”的大写或小写字母 到“z”。您不能使用此方法来 获取有关以下驱动器名的信息: nullNothingnullptra是否为null引用 (Visual Basic中无任何内容)或使用UNC (\server\share)路径

我能够通过在Windows资源管理器中映射网络驱动器使其工作。也就是说,我将“\server\share”映射到驱动器Z,然后
DriveInfo(“Z:\\”)给了我我所期望的


不幸的是,没有简单的方法从C#映射网络驱动器。您必须执行外部命令(即“net use z:\server\share”),或者调用Windows API函数来执行此操作。无论采用哪种方式,完成后都需要删除驱动器映射。

在Windows上,以下操作在C#中非常有效(至少要获得最常用的大小):

下面是一些示例代码(实际上并没有以这种形式编译,而是分散在多个文件中的工作代码片段):

//
///文件系统中文件夹和文件属性的汇编。
/// 
公共结构文件系统属性
{
专用FileSystemProperties(长?totalBytes、长?freeBytes、长?availableBytes)
:此()
{
TotalBytes=TotalBytes;
FreeBytes=FreeBytes;
AvailableBytes=可用字节;
}
/// 
///获取驱动器上的总字节数。
/// 
public long?TotalBytes{get;private set;}
/// 
///获取驱动器上的可用字节数。
/// 
public long?FreeBytes{get;private set;}
/// 
///获取驱动器上可用的字节数(计算磁盘配额)。
/// 
公共长可用字节{get;私有集;}
/// 
///获取此文件系统的属性。
/// 
///要查询其卷属性的路径。
///可用于取消操作的可选选项。
///包含指定文件系统的属性的。
公共静态FileSystemProperties GetProperties(字符串volumeIdentifier)
{
乌龙可用;
乌龙总数;
乌龙自由;
if(GetDiskFreeSpaceEx(volumeIdentifier,可用输出,总输出,可用输出))
{
返回新的FileSystemProperty((长)总计,(长)可用,(长)可用);
}
返回新的FileSystemProperty(null、null、null);
}
/// 
///异步获取此文件系统的属性。
/// 
///要查询其卷属性的路径。
///可用于取消操作的可选选项。
///包含此条目的名称的。
公共静态异步任务GetPropertiesAsync(字符串volumeIdentifier,CancellationToken cancel=default(CancellationToken))
{
返回等待任务。运行(()=>
{
乌龙可用;
乌龙总数;
乌龙自由;
if(GetDiskFreeSpaceEx(volumeIdentifier,可用输出,总输出,可用输出))
{
返回新的FileSystemProperty((长)总计,(长)可用,(长)可用);
}
返回新的FileSystemProperty(null、null、null);
},取消);
}
}
不要试着在Linux或Mac上使用它——它必须被重写(我很有兴趣看到这一点)。

为什么c#没有一个简单的unc路径方法,真是太傻了。若其他人稍后将某些内容映射到该驱动器号,那个么程序将失败。必须有一种仅使用unc路径的可靠方法
    [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);
/// <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);
    }
}