Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

System File Association

, 16 Mar 2007
This article describes the use of several custom C# classes that can be used to create, view, edit and delete Windows file associations.
/*
* Copyright (c) 2006, Brendan Grant (grantb@dahat.com)
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*     * All original and modified versions of this source code must include the
*       above copyright notice, this list of conditions and the following
*       disclaimer.
*     * This code may not be used with or within any modules or code that is 
*       licensed in any way that that compels or requires users or modifiers
*       to release their source code or changes as a requirement for
*       the use, modification or distribution of binary, object or source code
*       based on the licensed source code. (ex: Cannot be used with GPL code.)
*     * The name of Brendan Grant may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY BRENDAN GRANT ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL BRENDAN GRANT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace BrendanGrant.Helpers.FileAssociation
{
   /// <summary>
   /// Provides more streamlined interface for associating a single or multiple extensions with a single program.
   /// </summary>
   class AssociationManager
   {
      /// <summary>
      /// Determines of the list of extensions are associated with the specified program id.
      /// </summary>
      /// <param name="progId">Program id to check against.</param>
      /// <param name="extensions">String array of extensions to check against the program id.</param>
      /// <returns>String array of extensions that were not associated with the program id.</returns>
      public string[] CheckAssociation(string progId, params string[] extensions)
      {
         List<string> notAssociated = new List<string>();

         foreach (string s in extensions)
         {
            FileAssociationInfo fai = new FileAssociationInfo(s);

            if (!fai.Exists || fai.ProgID != progId)
               notAssociated.Add(s);
         }

         return notAssociated.ToArray();

      }
      
      /// <summary>
      /// Associates a single executable with a list of extensions.
      /// </summary>
      /// <param name="progId">Name of program id</param>
      /// <param name="executablePath">Path to executable to start including arguments.</param>
      /// <param name="extensions">String array of extensions to associate with program id.</param>
      /// <example>progId = "MyTextFile"
      /// executablePath = "notepad.exe %1"
      /// extensions = ".txt", ".text"</example>
      public void Associate(string progId, string executablePath, params string[] extensions )
      {
         foreach (string s in extensions)
         {
            FileAssociationInfo fai = new FileAssociationInfo(s);

            if (!fai.Exists)
               fai.Create(progId);

            fai.ProgID = progId;
         }

         ProgramAssociationInfo pai = new ProgramAssociationInfo(progId);

         if (!pai.Exists)
            pai.Create();

         pai.AddVerb(new ProgramVerb("open", executablePath));
      }

      /// <summary>
      /// Associates an already existing program id with a list of extensions.
      /// </summary>
      /// <param name="progId">The program id to associate extensions with.</param>
      /// <param name="extensions">String array of extensions to associate with program id.</param>
      public void Associate(string progId, params string[] extensions)
      {
         foreach (string s in extensions)
         {
            FileAssociationInfo fai = new FileAssociationInfo(s);

            if (!fai.Exists)
               fai.Create(progId);

            fai.ProgID = progId;
         }
      }

   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Brendan Grant
Web Developer
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 16 Mar 2007
Article Copyright 2007 by Brendan Grant
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid