翻译水平低磁盘名称为高电平磁盘名称 [英] Translate Low level disk name to high level disk name

查看:179
本文介绍了翻译水平低磁盘名称为高电平磁盘名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个基本的诊断工具,以显示瓶颈在哪里在他们的计算机水平较高的用户(让他们明白,得到更多的RAM /更快的处理器不会解决他们的问题)。我一直依赖相当严重的性能计数器类.NET环境中,到目前为止,它一直运作良好。



然而,当与逻辑磁盘驱动器的工作我已经遇到轻微的问题。在我的电脑上我有一个网络驱动器,为办公的文档共享驱动器(Z),但是性能计数器是指该驱动器为HarddiskVolume2。我知道,在引擎盖下,这是什么逻辑驱动器实际上是命名,别名为Z:实际上只是为用户的利益,但用户不会知道什么是HarddiskVolume2是,如果我离开它<。 / p>

有什么办法来翻译HarddiskVolume2到Z使用任何系统调用?


解决方案

如果你想看到你的所有映射驱动器及其解决路径的列表:

 控制台。的WriteLine(的string.join(Environment.NewLine,
GetUNCDrives()选择(KVP =方式>
的String.Format({0} => {1},kvp.Key,KVP。值))));

如果你想要得到的一个路径可能更短的分辨率列表:

  VAR longPath = @\\HarddiskVolume2\ent\RRPSTO\foobar\file.txt 

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

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

  VAR shortPaths = GetShortPaths(longPath); 
VAR路径= shortPaths.Length> 0? shortPaths [0]:longPath;



或者,你可以从列表基于网络地图有多深挑选。因此,要获得的最短路径映射(不是最短路径名),你只算多少/是路径。



或者你也可以作弊,只是采取一个用最短路径名。这并不能保证是最简单的路径,但。



但是要做到这一点,下面的代码是什么使上述工作的代码。
你需要这些家伙:

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Management;
:使用System.IO;

和您还需要包括 System.Management.dll 包含在您的项目引用



代码:

  ///<总结>获取路径提供所有可能的路径更短阵< /总结> 
///< PARAM NAME =路径>该路径来寻找备用地址< /参数>
静态字符串[] GetShortPaths(字符串路径)
{
返回GetUNCDrives()
。凡(KVP => path.StartsWith(kvp.Value))
。选择(KVP = GT; Path.Combine(kvp.Key,path.Substring(kvp.Value.Length + 1)))
.ToArray();
}

///<总结>获取所有映射驱动器和路径解决与LT; /总结>
///<退货和GT;解释:主要=驱动器,值=解析的路径与LT; /回报>
静态字典<字符串,字符串> GetUNCDrives()
{
返回DriveInfo.GetDrives()
。凡(DI => di.DriveType == DriveType.Network)
.ToDictionary(DI => DI .RootDirectory.FullName
,二= GT; GetUNCPath(di.RootDirectory.FullName.Substring(0,2)));
}

///<总结>尝试解析路径/根映射值< /总结>
///< PARAM NAME =路径>该路径来解决< /参数>
静态字符串GetUNCPath(字符串路径)
{
如果(path.StartsWith(@\\))
返回路径;

VAR月=新的ManagementObject(的String.Format(=的Win32_LogicalDisk{0}',路径));

返回Convert.ToUInt32(MO [的DriveType])==(UInt32的)DriveType.Network
? Convert.ToString(MO [的ProviderName])
:路径;
}


I'm writing a basic diagnostic tool to show high level users where the bottlenecks are in their computers (so they understand that getting more ram/faster processor won't fix their problems). I've been relying fairly heavily on the Performance Counter class within the .NET environment and so far and it's been working well.

When working with Logical Disk Drives however I've come across a slight issue. On my computer I have a network drive for the office's shared documents drive (Z), however the performance counter refers to this drive as "HarddiskVolume2". I know that under the hood this is what the logical drive is actually named, and the alias to "Z:" is really just for the user's benefit, but the users won't know what "HarddiskVolume2" is if I leave it.

Is there any way to translate "HarddiskVolume2" to "Z" using any system calls?

解决方案

If you want to see a list of all your mapped drives and their resolved paths:

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

If you want to get a list of possible shorter resolutions for a path:

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

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

If you just assume there is only going to be one mapped drive resolution, you could, just select the first one:

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

Or you could pick from the list based on how deep the network map was. So to get the shortest mapping path (not shortest path name) you just count how many '/' are in the path.

Or you could cheat and just take the one with the shortest path name. This isn't guaranteed to be the most simple path though.

However you want to do it, the code below is what makes the code above work. You'll need these guys:

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

And you'll also need to include the System.Management.dll included in your project references.

Code:

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

这篇关于翻译水平低磁盘名称为高电平磁盘名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆