这里的问题是,一个人可能会将网络共享映射到Drive Y
,而组织中的其他人可能会将相同的网络共享映射到Drive X
;因此,发送一个像Y:\mydirectory
这样的链接,除了我之外,其他任何人都不一定能做到。
正如OP所解释的那样,资源管理器确实会在资源管理器栏中显示实际路径,但即使你从上下文菜单中选择copy as 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()));
}
}
}
}
这里是UNC path
类的定义(我不记得在哪里找到的,所以我会试着找到实际的来源)。
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:008-02-29"
/// might return: "\networkserver\Shares\Photos008-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;
}
}
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"
你建立程序,然后把可执行文件放在你的电脑里,比如说,在Pathing
里,现在你在资源管理器里添加一个上下文菜单选项,这样:
Regedit,然后:
&001
你就完成了。现在,当你从映射的驱动器中右击一个目录时,你会看到这样的选项:
注意
我可以提供可执行文件,这样你就不需要自己编译了。只要在这里给我留言就可以了。