C# 如何找到已安装软件的执行路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/909910/
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-08-06 02:36:07  来源:igfitidea点击:

how to find the execution path of a installed software

c#windows

提问by Arunachalam

How can i find the execution path of a installed software in c# for eg media player ,vlc player . i just need to find their execution path . if i have a vlc player installed in my D drive . how do i find the path of the VLC.exe from my c# coding

如何在 C# 中找到已安装软件的执行路径,例如媒体播放器、vlc 播放器。我只需要找到他们的执行路径。如果我的 D 驱动器中安装了 vlc 播放器。我如何从我的 c# 编码中找到 VLC.exe 的路径

采纳答案by RobV

This method works for any executable located in a folder which is defined in the windows PATH variable:

此方法适用于位于 Windows PATH 变量中定义的文件夹中的任何可执行文件:

private string LocateEXE(String filename)
{
    String path = Environment.GetEnvironmentVariable("path");
    String[] folders = path.Split(';');
    foreach (String folder in folders)
    {
        if (File.Exists(folder + filename))
        {
            return folder + filename;
        } 
        else if (File.Exists(folder + "\" + filename)) 
        {
            return folder + "\" + filename;
        }
    }

    return String.Empty;
}

Then use it as follows:

然后按如下方式使用它:

string pathToExe = LocateEXE("example.exe");

Like Fredrik's method it only finds paths for some executables

像 Fredrik 的方法一样,它只找到一些可执行文件的路径

回答by joshcomley

回答by Fredrik M?rk

Using C# code you can find the path for some excutables this way:

使用 C# 代码,您可以通过以下方式找到某些可执行文件的路径:

private const string keyBase = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths";
private string GetPathForExe(string fileName)
{
    RegistryKey localMachine = Registry.LocalMachine;
    RegistryKey fileKey = localMachine.OpenSubKey(string.Format(@"{0}\{1}", keyBase, fileName));
    object result = null;
    if (fileKey != null)
    {
        result = fileKey.GetValue(string.Empty);
        fileKey.Close();
    }


    return (string)result;
}

Use it like so:

像这样使用它:

string pathToExe = GetPathForExe("wmplayer.exe");

However, it may very well be that the application that you want does not have an App Paths key.

但是,很可能您想要的应用程序没有 App Paths 键。

回答by Kevin Newman

This stackoverflow.com articledescribes how to get the application associated with a particular file extension.

这篇 stackoverflow.com 文章描述了如何获取与特定文件扩展名关联的应用程序。

Perhaps you could use this technique to get the application associated with certain extensions, such as avi or wmv - either Medial Player or in your case VLC player?

也许您可以使用这种技术来获取与某些扩展(例如 avi 或 wmv)相关联的应用程序 - Medial Player 或在您的情况下是 VLC 播放器?

回答by Country Don

I used the CurrentVersion\Installer\Folders registry key. Just pass in the product name.

我使用了 CurrentVersion\Installer\Folders 注册表项。只需传入产品名称。

private string GetAppPath(string productName)
    {
        const string foldersPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders";
        var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);

        var subKey = baseKey.OpenSubKey(foldersPath);
        if (subKey == null)
        {
            baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
            subKey = baseKey.OpenSubKey(foldersPath);
        }
        return subKey != null ? subKey.GetValueNames().FirstOrDefault(kv => kv.Contains(productName)) : "ERROR";          
    }