Library code snippets

Associating your program with every file

We'll create a class ( ShellExtension ) that allows us to associate our program with all files. So, we'll have one public method for that, which I'll call AssociateProgram.

But first, some info about this.

Where are those entries stored? They all reside in the Registry, global ones live in HKEY_LOCAL_MACHINE and per-user ones in HKEY_CURRENT_USER. Our class will only use it per user, as we don't want to spam the system.

Here is a sample registry file that will add a "test" entry which will simply open notepad:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\*\shell\test\command]
@="notepad.exe"

Run it, and check it out. Not that useful, but it shows you the inner workings. Let's get started. We need Microsoft.Win32 to access the Registry.

using System;
using Microsoft.Win32;

namespace ShellExtension {
      public class ShellExtension {
            public ShellExtension() {    }
           
            public bool AssociateProgram(string extensionName, string filePath) {
           
            } /* AssociateProgram */
      } /* ShellExtension */
} /* ShellExtension */

We'll add two 'Helper' methods to read and create RegistryKeys . Someday we could do something when creating a key, like log it somewhere.

The true in our call to OpenSubKey below implies to open it for reading. Otherwise you'd get an UnAuthorizedException when creating our shell extension.

private RegistryKey GetKey(RegistryKey rootKey, string keyName) {
      return rootKey.OpenSubKey(keyName, true);
} /* GetKey */
     
private bool CreateKey(RegistryKey rootKey, string keyName) {
      return (!(rootKey.CreateSubKey(keyName) == null));
} /* CreateKey */

Now, we have to create that key. Because almost nobody has '*' in his CURRENT_USER.

This is the 'biggest' method, after we've made sure our key is there we only have to put our shell extension in there.

private RegistryKey SetupKeys() {
      RegistryKey returnKey = null;
     
      RegistryKey currentUser = Registry.CurrentUser;
      RegistryKey softwareKey = this.GetKey(currentUser, "Software");
      if (softwareKey != null) {
            RegistryKey softwareClasses = this.GetKey(softwareKey, "Classes");
            if (softwareClasses != null) {
                  RegistryKey allFiles = this.GetKey(softwareClasses, "*");
                  if (allFiles != null) {
                        RegistryKey shellExtension = this.GetKey(allFiles, "shell");
                        if (shellExtension != null) {
                              returnKey = shellExtension;
                        } else {
                              if (this.CreateKey(allFiles, "shell")) { returnKey = this.GetKey(allFiles, "shell"); }
                        }   
                  } else {
                        if (this.CreateKey(softwareClasses, "*")) {
                              allFiles = this.GetKey(softwareClasses, "*");
                              RegistryKey shellExtension = this.GetKey(allFiles, "shell");
                              if (shellExtension != null) {
                                    returnKey = shellExtension;
                              } else {
                                    if (this.CreateKey(allFiles, "shell")) { returnKey = this.GetKey(allFiles, "shell"); }
                              }   
                        }
                  } // HKEY_CURRENT_USER\Software\Classes\*
            } // HKEY_CURRENT_USER\Software\Classes
      } // HKEY_CURRENT_USER\Software
      return returnKey;
} /* SetupKeys */

The only thing left now is to add our extension caption along with the 'command' key and the filepath. We use "filepath" %1 to pass the filename along.

RegistryKey shellKey = this.SetupKeys();
if (shellKey != null) {
      if (this.CreateKey(shellKey, extensionName + @"\command")) {
            RegistryKey extPath = this.GetKey(shellKey, extensionName + @"\command");
            extPath.SetValue("", "\"" + filePath + "\" %1");
      }
}

That's it. Compile your class, reference the .dll and call it like this:

ShellExtension.ShellExtension shellAdd = new ShellExtension.ShellExtension();
shellAdd.AssociateProgram("Notepad", "notepad.exe");

I've uploaded the project again, the ShellExtensionDriver shows you how to associate Notepad with every file (which is very usefull!).

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Associating your program with every file.

Leave a comment

Sign in or Join us (it's free).

David Cumps

Related podcasts

  • Object-Oriented Programming in Ruby

    In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...

Want to stay in touch with what's going on? Follow us on twitter!