Welcome to MSDN Blogs Sign in | Join | Help

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
Published Wednesday, January 30, 2008 12:17 PM by mwade

Attachment(s): MWadePad.zip

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# MSDN Blog Postings &raquo; How To: Add File Associations to a ClickOnce Application

# re: How To: Add File Associations to a ClickOnce Application

Wednesday, January 30, 2008 6:21 PM by Jeff Bailey

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)

Sunday, February 03, 2008 3:00 PM by Bite my bytes

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

# re: How To: Add File Associations to a ClickOnce Application

Monday, February 04, 2008 5:05 PM by Etienne Tremblay

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

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

Wednesday, February 06, 2008 5:10 PM by Zunanji viri

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

# re: How To: Add File Associations to a ClickOnce Application

Tuesday, February 12, 2008 10:25 AM by SvenAkeA

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

# re: How To: Add File Associations to a ClickOnce Application

Monday, February 18, 2008 1:04 AM by Vk

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

# re: How To: Add File Associations to a ClickOnce Application

Friday, February 29, 2008 6:32 AM by Jonny

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"

 />

</asmv1:assembly>

Any idea?

Thanks a lot

# Tool: Editing File Associations for ClickOnce Applications

Friday, May 30, 2008 5:00 PM by Re-inventing The Wheel

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

# What's New in Visuall Studio 2008 SP1 ClickOnce Tooling

Friday, August 29, 2008 6:14 PM by Re-inventing The Wheel

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

# re: How To: Add File Associations to a ClickOnce Application

Wednesday, September 24, 2008 3:54 PM by Michael DePouw

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.

# re: How To: Add File Associations to a ClickOnce Application

Wednesday, January 07, 2009 8:07 PM by Nick Archibald

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?

# re: How To: Add File Associations to a ClickOnce Application

Thursday, March 26, 2009 11:46 AM by Stefan Lindblad

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.

# re: How To: Add File Associations to a ClickOnce Application

Thursday, July 09, 2009 1:18 PM by chris

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.

# re: How To: Add File Associations to a ClickOnce Application

Tuesday, July 28, 2009 3:07 AM by mmarolda

Thank you! It worked perfectly !!

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker