简体   繁体   English

如何知道exe的安装路径?

[英]How can I know installation path of a exe?

I'm making a C# program and I need to find installation paths of some software that is installed on a computer. 我正在制作C#程序,我需要找到计算机上安装的某些软件的安装路径。 What I have to work with is, I have the Program's name (eg Google Chrome), i have the process name (eg Chrome.exe). 我需要使用的是程序名称(例如Google Chrome),进程名称(例如Chrome.exe)。 What I need now is the path to Chrome.exe. 我现在需要的是Chrome.exe的路径。 How can i use C# to find the path if i was to pass either the program name or process name as a parameter for the search? 如果我要通过程序名或进程名作为搜索参数,如何使用C#查找路径? Actually I want to make a custom action which will find chrome.exe and invoke a link. 实际上,我想执行一个自定义操作,该操作将找到chrome.exe并调用一个链接。 After that I will use the path for search chrome.exe and I want to default open a website via chrome. 之后,我将使用搜索chrome.exe的路径,并且我想默认通过chrome打开一个网站。 What should I do..? 我该怎么办..?

Another option to consider is just launching the link using Process.Start() and letting the operating system use the default browser to open the link. 要考虑的另一个选项是使用Process.Start()启动链接,并让操作系统使用默认的浏览器打开链接。 That would likely be more what the user would expect. 那可能是用户期望的。

In the WiX toolset, you can get that behavior for free using ShellExecute standard custom action from the WixUtilExtension . 在WiX工具WixUtilExtension ,您可以使用WixUtilExtension ShellExecute标准自定义操作免费获得该行为。

You could try something like this 你可以尝试这样的事情

public string GetProcessPath(string name)
{
 Process[] processes = Process.GetProcessesByName(name);

 if (processes.Length > 0)
 {
    return processes[0].MainModule.FileName;
 }
 else
 {
    return string.Empty;
 }
}

or you could use Linq 或者您可以使用Linq

or you could do what you do but use linq

     Process element = ( from p in Process.GetProcesses()
                where p.ProcessName == "Chrome.exe"
                select p ).FirstOrDefault( );

However there can be multiple process with same name .So you have to further modify the code according to your requirement. 但是,可以有多个同名进程。因此,您必须根据需要进一步修改代码。

hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能获得前景窗口的exe路径 - How can i get the exe path of the foreground window 如何用未知路径调用.exe文件? - How can I call a .exe file W/unknown Path? 如何找到 C# 的 tesseract.exe 路径? - How can I find tesseract.exe path with C#? 如何以编程方式获取另一个应用程序的安装路径? - How can I get another application's installation path programmatically? 如何使用 c# 运行 exe 文件(我不能使用 Process.Start() 因为我不知道 exe 文件的位置) - How can I run an exe file with c# (I can't use Process.Start() because I don't know the exe file's location) 我如何获取当前路径而不是 c# 中的 exe 路径? - How can iI get the current path and not the path of exe in c#? 如何从自定义操作中获取 setup.exe 的路径? - How can I get path of setup.exe from custom action? 如何通过项目已发布的安装程序在 [PATH] 中安装 Selenium 的 webdriver.exe? - How can I install Selenium’s webdriver.exe in [PATH] htrough the project’s published installer? 在其他位置处理另一个文件时,如何以编程方式获取一个exe的路径 - how can i get the path to one exe programatically while working with another file at other location 如何设置预定义的安装路径,而不是用户定义的路径? - How can I set pre-defined installation path, not user defined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM