A little-known feature that made it into .NET Framework 3.5 is the ability for a ClickOnce application to be associated with document extensions. That is, once properly configured, double-clicking on a document can cause your ClickOnce application to launch. Right now, there is no direct tooling support for this feature (and if you ask me the MSDN information doesn't tell the whole story), but in this post I'll show you how you can get file associations added to your ClickOnce application.
Adding File Associations to Your ClickOnce Manifest
File associations are adding to your ClickOnce manifest by specifying the fileAssociation tag and it's 4 attributes: extension, description, progid, and defaultIcon. This new tag is in the clickonce.v1 namespace. So, one example looks like this:
A little background information about all of these:
- extension: This is the file extension for which your application will be associated. You should make sure that your extension adds the "." to the front of the file. You should aim to use a unique extension for your file association. If, at install time, ClickOnce determines that this file association is already being used, it won't add the necessary system information to register your file association.
- description: A brief statement describing the type of document. This appears in the tooltip when hovering over the document, as well as the Type column in explorer when looking at the document.
- progid: Helps tie the file extension to your ClickOnce application.
- defaultIcon: The icon file from the ClickOnce application to use as the document icon
Of course, adding file association information to your application manifest is only half of the battle. How can you tell if your application was launched by launching one of its documents? Well, this information is stored in the ActivationData of the current app domain:
Even if the application is activated through file association, the standard ClickOnce update servicing will be applied.
Adding File Associations through Visual Studio
There is no fancy UI to allow you to add file associations to your ClickOnce application in Visual Studio. It is also not possible to add the information to your application manifest after publishing, because that would invalidate the signature. So, does that mean you are forced to modify your manifest and then re-sign with mage after publishing?
Nope. When publishing in Visual Studio, it is possible to add a app.manifest file to your project. This app.manifest acts as an base application manifest, whose information gets added into the generated application manifest. The manifest can be added by clicking the "Enable ClickOnce Security Settings" checkbox on the Security property page.
For example, here is what my app.manifest file looks like when adding the information from above:
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="18.104.22.168" name="MyApplication.app" />
<PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
<defaultAssemblyRequest permissionSetReference="Custom" />
There are some rules and regulations that need to be pointed out for file associations in ClickOnce:
- Values must be given for all 4 xml attributes
- File associations only work for pure ClickOnce applications. That means that browser-hosted applications and Office documents can't take advantage
- The application must be targeting the 3.5 version of the .NET Framework
- The application must be full trust
- The application must be installed (not run from web)
- There is a limit of 8 file associations per application