我只是需要OP询问同样的事情,在Google上搜索并阅读答案后,他们都没有提供我认为OP和我正在寻找的东西。
这里的问题是,一个人可能将一个网络共享映射到;Drive Y
而组织中的其他人可能将相同的网络共享映射为Drive X
;因此,发送诸如这样的链接Y:\mydirectory
可能对我以外的其他任何人都无效。
正如OP所述,Explorer确实在Explorer栏中显示了实际路径,但是即使您copy as path
从上下文菜单中进行选择,您也无法复制它(键入很繁琐且容易出错,因此这不是一个选择):
因此,我想出的解决方案(通过复制其他人的代码)是一个小的C#程序,您可以从资源管理器的上下文菜单中调用它,并允许您将映射的驱动器号转换为实际的UNC path
。
这是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Utils
{
//This is the only piece of code I wrote
class Program
{
[STAThread]
static void Main(string[] args)
{
//Takes the parameter from the command line. Since this will
//be called from the context menu, the context menu will pass it
//via %1 (see registry instructions below)
if (args.Length == 1)
{
Clipboard.SetText(Pathing.GetUNCPath(args[0]));
}
else
{
//This is so you can assign a shortcut to the program and be able to
//Call it pressing the shortcut you assign. The program will take
//whatever string is in the Clipboard and convert it to the UNC path
//For example, you can do "Copy as Path" and then press the shortcut you
//assigned to this program. You can then press ctrl-v and it will
//contain the UNC path
Clipboard.SetText(Pathing.GetUNCPath(Clipboard.GetText()));
}
}
}
}
这是Pathing
类的定义(由于不记得在哪里找到了,所以我将尝试查找实际的源代码):
public static class Pathing
{
[DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int WNetGetConnection(
[MarshalAs(UnmanagedType.LPTStr)] string localName,
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName,
ref int length);
/// <summary>
/// Given a path, returns the UNC path or the original. (No exceptions
/// are raised by this function directly). For example, "P:\2008-02-29"
/// might return: "\\networkserver\Shares\Photos\2008-02-09"
/// </summary>
/// <param name="originalPath">The path to convert to a UNC Path</param>
/// <returns>A UNC path. If a network drive letter is specified, the
/// drive letter is converted to a UNC or network path. If the
/// originalPath cannot be converted, it is returned unchanged.</returns>
public static string GetUNCPath(string originalPath)
{
StringBuilder sb = new StringBuilder(512);
int size = sb.Capacity;
// look for the {LETTER}: combination ...
if (originalPath.Length > 2 && originalPath[1] == ':')
{
// don't use char.IsLetter here - as that can be misleading
// the only valid drive letters are a-z && A-Z.
char c = originalPath[0];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
int error = WNetGetConnection(originalPath.Substring(0, 2),
sb, ref size);
if (error == 0)
{
DirectoryInfo dir = new DirectoryInfo(originalPath);
string path = Path.GetFullPath(originalPath)
.Substring(Path.GetPathRoot(originalPath).Length);
return Path.Combine(sb.ToString().TrimEnd(), path);
}
}
}
return originalPath;
}
}
您生成程序并将可执行文件放入PC中的某个位置,例如, c:\Utils
现在,您可以在资源管理器中添加一个上下文菜单选项,如下所示:
Regedit,然后:
HKEY_CLASSES_ROOT\*\Directory\Shell
Right-click Shell --> New Key --> Name: "To UNC Path"
Right-click To UNC Path --> New Key --> Name: command
Right-click Default entry and select `Modify`
Value Data: c:\Utils\Utils.exe "%1"
大功告成 现在,当您从映射驱动器上右键单击目录时,将看到此选项:
注意
我可以提供可执行文件,因此您不必自己进行编译。只需在此处给我留言。