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.
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:
<fileAssociation xmlns="urn:schemas-microsoft-com:clickonce.v1" extension=".mwade" description="MWadePad Document" progid="MWadePad.Document" defaultIcon="mwade.ico"/>
A little background information about all of these:
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:
AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0]
Even if the application is activated through file association, the standard ClickOnce update servicing will be applied.
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="1.0.0.0" name="MyApplication.app" /> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> <security> <applicationRequestMinimum> <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" /> <defaultAssemblyRequest permissionSetReference="Custom" /> </applicationRequestMinimum> </security> </trustInfo>
<fileAssociation xmlns="urn:schemas-microsoft-com:clickonce.v1" extension=".mwade" description="MWadePad Document" progid="MWadePad.Document" defaultIcon="mwade.ico" />
</asmv1:assembly>
There are some rules and regulations that need to be pointed out for file associations in ClickOnce:
PingBack from http://msdnrss.thecoderblogs.com/2008/01/30/how-to-add-file-associations-to-a-clickonce-application/
Thanks, this helped me out. I was chasing this problem by trying to alter the manifest after the publish, then calling Mage.exe. This is much simpler.
Links of the Week #22 (week 5/2008)
Great post!! I was looking for a way to do that and now you gave it me on platter. It works great. You should mention that we need to check the content of AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0] before using it because it is empty if you start from the command line, etc. Also why is the file in that value and not in the args passed in main? It is very difficult to debug since we can't pass it as a param. Anyway good job explaining it.
On other thing, the icon file needs to be in a resource in your application and you need to point to that resource in the manifest like "resource\icon.ico" I go a weird error if I just used icon.ico and it was just a file in the project.
Cheers,
ET
Development How To: Add File Associations to a ClickOnce Application (via Greg ) - A cool new ClickOnce
Hi
Greate article.
I'm trying to make the association to a filetype already associated with another application. Do you know if there is any way of making this possible?
/SåA
How to do I acheive this throught Mage. I hve auto builds calling the Nant build internally calling mage. Can you please tell me how can I do this through NANT
Hi,
I tried this technique in my project. It works in the computer that I’m developing the application, but failed in other computers. The app.manifest is
<?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">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<applicationRequestMinimum>
<defaultAssemblyRequest permissionSetReference="Custom" />
<PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
</applicationRequestMinimum>
</security>
</trustInfo>
<fileAssociation xmlns="urn:schemas-microsoft-com:clickonce.v1"
extension=".mediastream"
description="Kangaroo Video Metainformation"
progid="Kangaroo.Document"
defaultIcon="Resources\kangaroo.ico"
/>
Any idea?
Thanks a lot
I apologize for taking so long between posts. I have an excuse: very busy working on Visual Studio 2008
Here is a post I've been meaning to do for a while, but avoided because there is some MSDN info on it.
Forgive me if this is stupid question but I can't think of a use for this feature, what would I use this for?
Does anyone have any examples of how they have used this feature they could share?
Thanks,
Mike D.
Nice Post, thanks. This is a very cool feature. Only thing is, I have found that the IExtractIcon interface is not being called on my companies shortcut handler to extract the icon from the shortcut file, it just uses the default icon given. Is this a limitation or am I missing something?
Thanks for this info. It put us right where we wanted to be in just a few minutes. Making it possible to open our own program with our own file extensions.
Also, the appliation must be a WindowsApplication. I tried with a CrystalReports application and it did not work. Also overwriting existing file associations did not work in my case.
Thank you! It worked perfectly !!