3

早上好,

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

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

... 当使用上面的 UNC 路径时,会引发 ArgumentException(“对象必须是根目录 (\"C:\\") 或驱动器号 (\"C\")。")。

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

4

3 回答 3

4

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

驱动器名称必须是从“a”到“z”的大写或小写字母。您不能使用此方法获取有关为 nullNothingnullptra 空引用(Visual Basic 中为 Nothing)或使用 UNC (\server\share) 路径的驱动器名称的信息。

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

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

于 2009-03-24T22:37:16.250 回答
0

在 Windows 上,以下在 C# 中效果很好(至少要获得最常用的尺寸):

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

不要试图在 Linux 或 Mac 上使用它——它必须为那些重写(我很想看看)。

于 2016-01-01T00:54:06.610 回答
0

这是一篇旧帖子,但两个答案都不是最简单的答案:阅读注册表并获取映射的驱动器 + UNC 路径。

注册表路径是 HKCU\Network。每个键都是映射的驱动器号,RemotePath 值是驱动器映射的 UNC 路径。

简单的注册表读取。

于 2022-01-25T10:56:22.533 回答