How can I associate a custom URL protocol with a ClickOnce app?

陌路散爱 提交于 2020-01-14 19:04:29

问题


I want to associate a custom URL protocol (for example, myprotocol://SomeFolder/SomePage) with a ClickOnce application.

I can create the association without a problem - the issue is that every time the application is updated (which is frequently) the path to the EXE file changes.

Is there any way around this issue?


回答1:


It seems the answer to this is you can't, however I did come up with a solution.

I created a launcher (very simple application) which finds the shortcut and passes its startup parameters to the ClickOnce application. I need to install the launcher in the traditional way, but the main application can still be updated via ClickOnce when needed.

I found these links useful:

  • How to pass arguments to an offline ClickOnce application

  • Registering an Application to a URL Protocol




回答2:


Once your application is installed system create a link in the start menu. The link is actually a file with "appref-ms" extension. So, the trick is to register a protocol that will use the "appref-ms" to open the app.

So, when you'r ClickOnce application starts you could create the following registry entry to register your protocol. HKEY_CLASSES_ROOT myprotocol = {Protocol Description} shell open command = explorer %1

That's it. Now when someone will click the url like myprotocol: XXX you'r app will be opened and will be opened "as ClickOnce" application so it will check if there is a new version and so on.




回答3:


If you're trying to ClickOnce already adds the appropriate key to HKCR, just without the required URL Protocol value. I added this code to the start of my application logic:

try
{
    RegistryKey rk = Registry.ClassesRoot.OpenSubKey("MyProgramName", true);
    rk.SetValue("URL Protocol", "");
}
catch (Exception ex)
{ 
    // handle, log, etc.
}

Which worked well because that's what I wanted the URL protocol to refer to (e.g "MyProgramName://....". I was able to do this successfully without my application having administrative rights - maybe it'd be required if I was trying to register a different handler though, so YMMV. At the very least, looking at the value in there should give you an idea of how to launch the application properly.

Here's the registry key that got created by default:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\MyAppName]
@="Electronic Data Interchange (EDI) File"
"AppId"="MyAppName.application, Culture=neutral, PublicKeyToken=31fc6d113f9bb401, processorArchitecture=msil"
"DeploymentProviderUrl"="file://server/share/MyAppName/MyAppName.application"
"Guid"="{MY_APP_GUID}"

[HKEY_CLASSES_ROOT\MyAppName\shell]
@="open"

[HKEY_CLASSES_ROOT\MyAppName\shell\open]

[HKEY_CLASSES_ROOT\MyAppName\shell\open\command]
@="rundll32.exe dfshim.dll, ShOpenVerbExtension {MY_APP_GUID} %1"

[HKEY_CLASSES_ROOT\MyAppName\shellex]

[HKEY_CLASSES_ROOT\MyAppName\shellex\IconHandler]
@="{MY_APP_GUID}"

And the code I've posted simply adds a URL Protocol with an empty value under the MyAppName node.



来源:https://stackoverflow.com/questions/7334460/how-can-i-associate-a-custom-url-protocol-with-a-clickonce-app

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