当 Windows 能够找到它时,如何找到 Mercurial 可执行文件的完整路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4139306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:35:13  来源:igfitidea点击:

How to find the full path to the Mercurial executable, when Windows is able to locate it?

.netwindowspathexecutable

提问by Lasse V. Karlsen

To clarify: The question is really: How do I locate the Mercurial command line client. If the answer is applicable to any executable, so much the better, but I'm really interested in the hg.exeexecutable file.

澄清一下:问题实际上是:我如何找到 Mercurial 命令行客户端。如果答案适用于任何可执行文件,那就更好了,但我真的对hg.exe可执行文件感兴趣。

If I know the name of an executable, say hg.exe, the Mercurial command line client, and Windows knows where it is because I can execute just hg logfrom a command prompt and it executes, what steps are involved in order for me to find that executable myself, in the same manner that the command prompt and Windows does it?

如果我知道一个可执行文件的名称,比如hg.exeMercurial 命令行客户端,并且 Windows 知道它在哪里,因为我可以只hg log从命令提示符执行并执行,为了让我自己找到该可执行文件,需要执行哪些步骤,与命令提示符和 Windows 的方式相同吗?

Basically, if Windows is able to locate it, I want my program to be able to locate it.

基本上,如果 Windows 能够找到它,我希望我的程序能够找到它。

Is there a WinAPI function, or similar? The code will run in .NET, written in C#, so if there's anything built into .NET for this that would be the preferred solution, but otherwise I'm not adverse to using P/Invoke for this.

是否有 WinAPI 函数或类似函数?代码将在 .NET 中运行,用 C# 编写,所以如果在 .NET 中为此内置了任何内容,那将是首选解决方案,但除此之外,我不反对为此使用 P/Invoke。

I've seen one potential duplicate of this question: c# Check if an executable exists in the windows path, but is that all there is to it? Just iterate over the contents of the PATHenvironment variable and looking in each of those directories for the executable?

我已经看到了这个问题的一个潜在重复:c# Check if an executable exists in the windows path,但仅此而已吗?只需遍历PATH环境变量的内容并在每个目录中查找可执行文件?

I have a vague notion that that's just one of the steps involved, and possibly that there are registry overrides that Windows can use that I should be aware of, so I'll post the question here.

我有一个模糊的概念,即这只是所涉及的步骤之一,并且可能存在我应该注意的 Windows 可以使用的注册表覆盖,因此我将在此处发布问题。

If, on the other hand, there really is just the PATH variable in play here, it can probably safely be closed as a duplicate.

另一方面,如果这里真的只有 PATH 变量在起作用,它可能可以安全地作为副本关闭。

采纳答案by jcolebrand

It depends on how the program is registered with the system. Since hg is generally run from either tools or the command line, it's not going to be registered with the system. If it were there's a set of registry keys that has the exe name and path. Otherwise, you just iter the path from the first entry to the last till you find the file you need. First one found on the path wins.

这取决于程序如何在系统中注册。由于 hg 通常从工具或命令行运行,因此不会在系统中注册。如果是,则有一组具有 exe 名称和路径的注册表项。否则,您只需迭代从第一个条目到最后一个条目的路径,直到找到所需的文件。在路径上找到的第一个获胜。

Examples of such a "registered" program, excel or winword.

此类“注册”程序的示例,excel 或 winword。

EDIT:

编辑:

@BillyONeal makes a good point below, that only works for "run" command programs, but my point was there was a second place to look.

@BillyONeal 在下面提出了一个很好的观点,它仅适用于“运行”命令程序,但我的观点是还有第二个地方可以查看。

Additionally, for those who haven't seen this, here's the install procedures:

此外,对于那些还没有看过这个的人,这里是安装过程

An alternative scheme that works better for some is to search for hg on the PATH

对某些人更有效的替代方案是在 PATH 上搜索 hg

回答by Greg Buehler

you can cheat and use the where.execommand

你可以作弊并使用where.exe命令

public GetFullPath(string program)
{
    string result;

    Process myProcess = new Process()
    {
        UseShellExecute = false,
        RedirectStandardOutput = true,
        StartInfo = new ProcessStartInfo(@"%SYSTEMDIR%\where.exe" )
    };

    using (StreamReader sr = myProcess.StandardOutput)
    {
        myProcess.Start();
        result = myStreamReader.ReadLine();
        myProcess.Close();
    }

    return result;
}

回答by Steve Townsend

Executables load according to the first matching instance in the system path. If executed from a shortcut or other mode that uses an absolute path, of course that's the version that runs, though.

可执行文件根据系统路径中的第一个匹配实例加载。如果从使用绝对路径的快捷方式或其他模式执行,当然这是运行的版本。

DLLs are a bit more complicated - for native DLLs there are overrides, perhaps that's what you are thinking about? See here.

DLL 有点复杂 - 对于本机 DLL 有覆盖,也许这就是您正在考虑的?见这里

回答by Michael

Windows provides the SearchPathfunction. If you pass NULLas the lpPathparameter, it uses the system search path. In your case you should call:

Windows 提供了该SearchPath功能。如果NULL作为lpPath参数传递,它将使用系统搜索路径。在您的情况下,您应该致电:

SearchPath(NULL, "hg", NULL, ...)

The C# declaration is:

C# 声明是:

[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern int SearchPath(string path, string fileName, string extension, int numBufferChars, StringBuilder buffer, int[] filePart);