【问题标题】:Translate Low level disk name to high level disk name将低级磁盘名称转换为高级磁盘名称
【发布时间】:2016-05-29 09:35:39
【问题描述】:

我正在编写一个基本的诊断工具来向高级用户展示他们计算机中的瓶颈所在(因此他们明白,获得更多内存/更快的处理器不会解决他们的问题)。我一直相当依赖 .NET 环境中的 Performance Counter 类,到目前为止,它运行良好。

在使用逻辑磁盘驱动器时,我遇到了一个小问题。在我的计算机上,我有一个用于办公室共享文档驱动器 (Z) 的网络驱动器,但是性能计数器将此驱动器称为“HarddiskVolume2”。我知道这实际上是逻辑驱动器的名称,“Z:”的别名实际上只是为了用户的利益,但如果我离开它,用户将不知道“HarddiskVolume2”是什么。

有没有办法使用任何系统调用将“HarddiskVolume2”转换为“Z”?

【问题讨论】:

  • @Jay,我不认为这是重复的。这个问题是关于 C++ 的,我的是 C#。例如,如果你在 python 中找到了我的问题的答案,你会称它为重复吗?
  • @HansPassant 该文档对这些函数的实际作用 有所了解,但看起来很有希望。我试试看。
  • @Jrud:C# 语言没有做你想做的事的功能。该功能在 platform API 中,它充其量与语言无关(实际上,与 C/C++ 密切相关)。现在,也就是说……建议的“重复”专门针对网络共享,而您的问题的标题似乎表明您可能对更一般的答案感兴趣,而不仅仅是处理网络共享(您的问题并不清楚,因为您使用的示例 is 关于网络共享)。如果您改进问题,请记住现有信息可能不在 C# 中。

标签: c# performancecounter hard-drive


【解决方案1】:

如果您想查看所有映射驱动器及其解析路径的列表:

Console.WriteLine(String.Join(Environment.NewLine,
    GetUNCDrives().Select(kvp => 
        string.Format("{0} => {1}", kvp.Key, kvp.Value))));

如果您想获取路径的可能更短分辨率的列表:

var longPath = @"\\HarddiskVolume2\ent\RRPSTO\foobar\file.txt";

Console.WriteLine(string.Join(Environment.NewLine, GetShortPaths(longPath)));

如果您只是假设只有一个映射驱动器分辨率,您可以选择第一个:

var shortPaths = GetShortPaths(longPath);
var path = shortPaths.Length > 0 ? shortPaths[0] : longPath;

或者您可以根据网络地图的深度从列表中进行选择。因此,要获得最短的映射路径(不是最短路径名),您只需计算路径中有多少“/”。

或者你可以作弊,只取路径名最短的那个。不过,这并不能保证是最简单的路径。

无论您想怎么做,下面的代码都是使上面的代码工作的原因。 你需要这些人:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.IO;

您还需要在项目引用中包含System.Management.dll

代码:

/// <summary>Gets array of all possible shorter paths for provided path.</summary>
/// <param name="path">The path to find alternate addresses for.</param>
static string[] GetShortPaths(string path)
{
    return GetUNCDrives()
        .Where(kvp => path.StartsWith(kvp.Value))
        .Select(kvp => Path.Combine(kvp.Key, path.Substring(kvp.Value.Length + 1)))
        .ToArray();
}

/// <summary>Gets all mapped drives and resolved paths.</summary>
/// <returns>Dictionary: Key = drive, Value = resolved path</returns>
static Dictionary<string, string> GetUNCDrives()
{
    return DriveInfo.GetDrives()
        .Where(di => di.DriveType == DriveType.Network)
        .ToDictionary(di => di.RootDirectory.FullName
                    , di => GetUNCPath(di.RootDirectory.FullName.Substring(0, 2)));
}

/// <summary>Attempts to resolve the path/root to mapped value.</summary>
/// <param name="path">The path to resolve.</param>
static string GetUNCPath(string path)
{
    if (path.StartsWith(@"\\"))
        return path;

    var mo = new ManagementObject(string.Format("Win32_LogicalDisk='{0}'", path));

    return Convert.ToUInt32(mo["DriveType"]) == (UInt32)DriveType.Network 
            ? Convert.ToString(mo["ProviderName"]) 
            : path;
}

【讨论】:

    猜你喜欢
    • 2023-01-15
    • 2020-03-31
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 2018-02-19
    • 2019-03-11
    相关资源
    最近更新 更多