问题出现在以下方面:
我创建了一个虚拟驱动器或“虚拟驱动器盘符”,在DOS下用一个叫做subst的命令通过这样的蝙蝠创建:
@echo off
cd \
subst J: /D
subst J: C:\Virtual_Disk\Unit_J
在Windows单元列表中,我看到创建的单元,其中放置了一些文件和一个可执行文件。我目前正在c#中开发一个程序,它调用虚拟路径中的可执行文件的路径,然后突然抛出异常,指示即使可执行文件在那里也找不到可执行文件的路径,查看可执行文件的属性。可执行文件和字段位置指示该可执行文件的虚拟路径,但不指示物理路由。如果我不清楚,我会举一个例子:
(1)可执行文件的虚拟路径
J:\Program\executable.exe
(2)可执行文件所在的物理路径
C:\Virtual_Disks\Unit_J\Program\executable.exe
C#捕获可执行文件的虚拟路径(1),但不捕获它的真实位置(2)。
没有更多,我感谢所提供的帮助。
答案 0 :(得分:0)
这应该起作用,来自here:
[DllImport("kernel32.dll")]
private static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax);
private static string GetPhysicalPath(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("path");
}
// Get the drive letter
var pathRoot = Path.GetPathRoot(path);
if (string.IsNullOrEmpty(pathRoot))
{
throw new ArgumentNullException("path");
}
var substPrefix = @"\??\";
var lpDeviceName = pathRoot.Replace(@"\", string.Empty);
var lpTargetPath = new StringBuilder(260);
if (QueryDosDevice(lpDeviceName, lpTargetPath, lpTargetPath.Capacity) != 0)
{
string result;
// If drive is substed, the result will be in the format of "\??\C:\RealPath\".
if (lpTargetPath.ToString().StartsWith(substPrefix))
{
// Strip the \??\ prefix.
var root = lpTargetPath.ToString().Remove(0, substPrefix.Length);
result = Path.Combine(root, path.Replace(Path.GetPathRoot(path), string.Empty));
}
else
{
// if not SUBSTed, just assume it's not mapped.
result = path;
}
return result;
}
else
{
return null;
}
}