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

发布于 2024-10-01 14:54:52 字数 768 浏览 2 评论 0原文

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

如果我知道可执行文件的名称,例如 Mercurial 命令行客户端 hg.exe,Windows 就知道它在哪里,因为我可以从命令中执行 hg log提示符并执行,为了让我自己找到该可执行文件,需要采取哪些步骤,就像命令提示符和 Windows 那样?

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

有 WinAPI 函数或类似函数吗?该代码将在 .NET 中运行,用 C# 编写,因此如果 .NET 中内置了任何为此目的的内容,那么这将是首选解决方案,但除此之外,我并不反对使用 P/Invoke 来实现此目的。

我看到了这个问题的一个潜在重复: c#检查 Windows 路径中是否存在可执行文件,但这就是全部吗?只需迭代 PATH 环境变量的内容并在每个目录中查找可执行文件?

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

另一方面,如果这里确实只有 PATH 变量在起作用,那么它可能可以安全地作为重复项关闭。

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.exe executable file.

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 log from 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?

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

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.

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 PATH environment variable and looking in each of those directories for the executable?

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无敌元气妹 2024-10-08 14:54:52

你可以作弊并使用 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;
}

you can cheat and use the where.exe command

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;
}
陈独秀 2024-10-08 14:54:52

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

这种“注册”程序的示例有 excel 或 winword。

编辑:

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

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

另一种对某些人来说效果更好的方案是在 PATH 上搜索 hg

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.

Examples of such a "registered" program, excel or 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.

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

傲影 2024-10-08 14:54:52

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

DLL 有点复杂——对于本机 DLL 来说有覆盖,也许这就是您正在考虑的?请参阅此处

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.

向地狱狂奔 2024-10-08 14:54:52

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

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

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);

Windows provides the SearchPath function. If you pass NULL as the lpPath parameter, it uses the system search path. In your case you should call:

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

The C# declaration is:

[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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文