Detecting browsers installed so Process.Start(“chrome”) won't error

爱⌒轻易说出口 提交于 2019-12-22 06:56:18

问题


I'm trying to let the user choose the browser my application uses to launch urls. Currently it uses the default browser, but some people want to specify a different browser.

I'd like to show only installed browsers in the list, and I'm launching them like this:

Process.Start("chrome", url);

The problem is, if Chrome isn't installed (and in the path), it'll fail.

How can I check whether this call will fail, without calling it (so I can pre-filter my list, and remove chrome if it's not going to work)?


回答1:


In Windows all of the installed applications have a key in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths registry key. One solution would be to iterate over all the entries in this key and see if they match the names of your supported browsers.

Once you've got the registry keys for each browser, you can then get the Path value of each key, and see if the executable file exists in the specified path.

One thing to note is that on 64-bit versions of Windows, 32-bit apps are listed in the HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\App Paths.




回答2:


You could wrap Process.Start("chrome", url); it in a try/catch (catching the exception thrown when browser is not installed)

Kindness,

Dan



来源:https://stackoverflow.com/questions/1887677/detecting-browsers-installed-so-process-startchrome-wont-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!