public void associateFileExtension(
String extension,
String pathToExecute,
String applicationName)
{
//' extension is three letters without the "."
//' pathToExecute is full path to exe file
//' application Name is any name you want as description of Extension
String sKeyName; // Holds Key Name in registry.
String sKeyValue; // Holds Key Value in registry.
if (!extension.Contains("."))
{
//' This creates a Root entry for the extension to be associated with ' ApplicationName' .
sKeyName = "." + extension;
sKeyValue = applicationName;
RegistryKey rKey = Registry.ClassesRoot.CreateSubKey(sKeyName);
rKey.SetValue(sKeyName, sKeyValue);
//' This creates a Root entry called ' ApplicationName' .
sKeyName = applicationName;
sKeyValue = applicationName;
RegistryKey rKeyApp = Registry.ClassesRoot.CreateSubKey(sKeyName);
rKeyApp.SetValue(sKeyName, sKeyValue);
//' This sets the command line for ' ApplicationName' .
sKeyName = applicationName;
sKeyValue = "\"" + pathToExecute + "\" %1";
rKey = rKeyApp.CreateSubKey("shell\\open\\command");
rKey.SetValue(sKeyName, sKeyValue);
//' This sets the default icon
sKeyName = applicationName;
sKeyValue = "\"" + pathToExecute + "\",0";
rKey = rKeyApp.CreateSubKey("DefaultIcon");
rKey.SetValue(sKeyName, sKeyValue);
SHChangeNotify(HChangeNotifyEventID.SHCNE_ASSOCCHANGED, HChangeNotifyFlags.SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);
}
}