How To: Add File Associations to a ClickOnce Application


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:



<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:



  • 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.Description Tooltip

  • 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:


AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0]


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="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>


Additional Notes


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

MWadePad.zip


Comments (17)

  1. Jeff Bailey says:

    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.  

  2. Links of the Week #22 (week 5/2008)

  3. 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 "resourceicon.ico" I go a weird error if I just used icon.ico and it was just a file in the project.

    Cheers,

    ET

  4. Zunanji viri says:

    Development How To: Add File Associations to a ClickOnce Application (via Greg ) – A cool new ClickOnce

  5. SvenAkeA says:

    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

  6. Vk says:

    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

  7. Jonny says:

    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"&gt;

     <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="Resourceskangaroo.ico"

     />

    </asmv1:assembly>

    Any idea?

    Thanks a lot

  8. I apologize for taking so long between posts. I have an excuse: very busy working on Visual Studio 2008

  9. Here is a post I’ve been meaning to do for a while, but avoided because there is some MSDN info on it.

  10. Michael DePouw says:

    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.

  11. Nick Archibald says:

    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?

  12. Stefan Lindblad says:

    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.

  13. chris says:

    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.

  14. mmarolda says:

    Thank you! It worked perfectly !!

  15. Takis says:

    Thank you for posting this information, it was really helpful.

  16. chris says:

    This solution does not work with already registered file types such as text files (txtfile associated with notepad.exe).

Skip to main content