我知道以前也有人问过类似的问题,但所有的答案都是片面的,例如,建议我使用我正在做的AssocQueryString
。然而,我仍然找不到一些可执行文件。例如,下面的代码无法找到outlook.exe或firefox.exe -在Windows Explorer地址栏中键入outlook.exe几乎可以立即找到它们。
在下面的代码中,exe fileName可以是用户机器上的任何本地位置,也可以不在用户搜索路径上。
我如何才能改进这一点以找到文件?(这是从32位exe调用的)
// for example, get actual full path to outlook.exe
string fullPath = FindPath("outlook.exe");
public static string FindPath(string fileName)
{
uint length = UnsafeMethods.MAX_PATH;
StringBuilder path = new StringBuilder((int)length);
if (Path.GetDirectoryName(fileName) == string.Empty)
{
if (UnsafeMethods.AssocQueryString(UnsafeMethods.AssocF.OpenByExeName, UnsafeMethods.AssocStr.Executable, fileName, null, path, ref length) != 0)
{
IntPtr filePart = IntPtr.Zero;
IntPtr wow64Value = IntPtr.Zero;
// try 64 bit path
UnsafeMethods.Wow64DisableWow64FsRedirection(ref wow64Value);
bool success = UnsafeMethods.SearchPath(null, fileName, null, path.Capacity, path, ref filePart) > 0;
UnsafeMethods.Wow64RevertWow64FsRedirection(wow64Value);
if (!success)
{
// try 32 bit path
success = UnsafeMethods.SearchPath(null, fileName, null, path.Capacity, path, ref filePart) > 0;
}
}
return path.ToString();
}
else
{
return fileName;
}
}
发布于 2019-03-20 04:33:12
似乎有很多地方可以找到通向exe的路径。虽然上面的原始代码可以工作,但这并不是一个详尽的搜索,您还需要查看注册表项SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\
并检查SpecialFolders Windows
,System
和SystemX86
(https://docs.microsoft.com/en-us/dotnet/api/system.environment.specialfolder?view=netframework-4.7.2)
https://stackoverflow.com/questions/55232487
复制相似问题