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
-
Query Tool to Excel using C# and .NET
by BarbaMariolino (1 replies)
-
looking for help on asp
by cladironbeard (2 replies)
-
Socket Programming in C# - Part 1
by graumanoz (23 replies)
-
LINQ in Action
by naser1 (0 replies)
-
Creating a Windows Service in VB.NET
by Templario55 (107 replies)
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...
Events coming up
-
Aug
28
St. Louis Day of .NET
St. Charles, United States
Technical conference with be 2 full days of content with over 40 sessions from local and national speakers, with topics such as:•.NET languages: C#, VB.NET•Technologies: WPF, Silverlight, WCF•Development tools: Visual Studio, TFS, Expression Blend
This thread is for discussions of Associating your program with every file.