Delen via


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="https://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

  • Anonymous
    January 30, 2008
    PingBack from http://msdnrss.thecoderblogs.com/2008/01/30/how-to-add-file-associations-to-a-clickonce-application/

  • Anonymous
    January 30, 2008
    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.  

  • Anonymous
    February 03, 2008
    Links of the Week #22 (week 5/2008)

  • Anonymous
    February 04, 2008
    The comment has been removed

  • Anonymous
    February 06, 2008
    Development How To: Add File Associations to a ClickOnce Application (via Greg ) - A cool new ClickOnce

  • Anonymous
    February 12, 2008
    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

  • Anonymous
    February 17, 2008
    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

  • Anonymous
    February 28, 2008
    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="Resourceskangaroo.ico"  /> </asmv1:assembly> Any idea? Thanks a lot

  • Anonymous
    May 30, 2008
    I apologize for taking so long between posts. I have an excuse: very busy working on Visual Studio 2008

  • Anonymous
    August 29, 2008
    Here is a post I've been meaning to do for a while, but avoided because there is some MSDN info on it.

  • Anonymous
    September 24, 2008
    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.

  • Anonymous
    January 07, 2009
    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?

  • Anonymous
    March 26, 2009
    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.

  • Anonymous
    July 09, 2009
    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.

  • Anonymous
    July 27, 2009
    Thank you! It worked perfectly !!

  • Anonymous
    January 14, 2010
    Thank you for posting this information, it was really helpful.

  • Anonymous
    March 25, 2010
    This solution does not work with already registered file types such as text files (txtfile associated with notepad.exe).