Library code snippets
Associating your program with every file
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 */
"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");
}
}
.dll and call it like this:
ShellExtension.ShellExtension shellAdd = new ShellExtension.ShellExtension();
shellAdd.AssociateProgram("Notepad", "notepad.exe");
ShellExtensionDriver shows you how to associate Notepad with every file (which is very usefull!).
Related articles
Related discussion
-
Installing windows service using MSI
by chirpyanu (0 replies)
-
Class, Abstract or Interface ?
by uzay95 (0 replies)
-
C++ dll for USB download in C# application
by sumahs (0 replies)
-
a bit of an abstract question about Test Driven Development
by gtejeda (0 replies)
-
An Introduction to VB.NET and Database Programming
by bitkisel (13 replies)
Related podcasts
-
Looking into the C# Crystal Ball with Charlie Calvert and Bill Wagner
One of the most exciting announcements from PDC was the news about C# 4.0 and Visual Studio 2010. With all the excitement and discussion throughout the event about these new developer tools, we reached out to two experts in the fields. Charlie Calvert and Bill Wagner sat down with Keith and Woody...
Events coming up
-
Jun
16
Code Generation 2009
Cambridge, United Kingdom
A developer event with a practical focus on helping people get to grips with code generation tools and technologies.
This thread is for discussions of Associating your program with every file.