如何查找已安装软件的执行路径

时间:2022-11-25 10:23:34

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的路径

5 个解决方案

#1


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的方法一样,它只找到一些可执行文件的路径

#2


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键。

#3


Have a look at MsiEnumProductsEx

看看MsiEnumProductsEx

#4


This *.com article describes how to get the application associated with a particular file extension.

此*.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播放器?

#5


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";          
    }

#1


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的方法一样,它只找到一些可执行文件的路径

#2


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键。

#3


Have a look at MsiEnumProductsEx

看看MsiEnumProductsEx

#4


This *.com article describes how to get the application associated with a particular file extension.

此*.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播放器?

#5


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";          
    }