在地图驱动器上获取当前可执行文件目录

我有一个地图驱动器在位置Z:\映射到\\ server1 \共享
现在我的可执行文件位于\\ server1 \ shared \ exe \ myExec.exe

我努力了

Directory.GetCurrentDirectory(); Environment.CurrentDirectory; Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); System.AppDomain.CurrentDomain.BaseDirectory 

他们都返回"\\server1\shared\exe"

有没有办法让我的结果是"Z:\exe\"

我结束了这两个功能。

UNC2LocalPath获取UNC路径并返回本地路径。 它遍历所有在本地机器上映射的驱动器。 如果找到匹配项,则用驱动器号代替服务器名称。

必需的参考System.Management.dll

 using System.Management; public static String UNC2LocalPath(String input) { DriveInfo[] dis = DriveInfo.GetDrives(); foreach (DriveInfo di in dis) { if (di.DriveType == DriveType.Network) { DirectoryInfo dir = di.RootDirectory; var unc = GetUNCPath(dir.FullName.Substring(0, 2)); if (input.StartsWith(unc_path, StringComparison.OrdinalIgnoreCase)) { return input.Replace(Path.GetPathRoot(unc) + "\\", dir.FullName); } } } return null; } public static string GetUNCPath(string path) { if (path.StartsWith(@"\\")) return path; ManagementObject mo = new ManagementObject(); mo.Path = new ManagementPath(string.Format("Win32_LogicalDisk='{0}'", path)); //DriveType 4 = Network Drive if (Convert.ToUInt32(mo["DriveType"]) == 4) return Convert.ToString(mo["ProviderName"]); else return path; } 

函数GetUNCPath取自GetUNCPath的答案@ 如何确定映射驱动器的实际路径?