[英]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.