first time posting in StackOverflow. :D I need my software to add a couple of things in the registry.
My program will use
Process.Start(@"blblabla.smc");
to launch a file, but the problem is that most likely the user will not have a program set as default application for the particular file extension.
How can I add file associations to the WindowsRegistry?
Using Python:
Associating an icon with the extension type:
Register the extension as an executable type (i.e. PATHEXT):
Additionally, to get PATHEXT recognised without logging in again you can update the environment: (thanks to Enthought for this)
Use the
Registry
class inMicrosoft.Win32
.Specifically, you want the
ClassesRoot
property ofRegistry
to access theHKEY_CLASSES_ROOT
key (cf. Understanding MS Windows File Associations and HKEY_CLASSES_ROOT: Core Services).Replace
"SMCProcessor \"%1\""
with the command-line path and argument specification for the program that you wish to associate with files with extension.smc
.But, instead of messing with the registry, why not just say
In addition to the answers already provided, you can accomplish this by calling the command line programs "ASSOC" and "FTYPE". FTYPE associates a file type with a program. ASSOC associates a file extension with the file type specified through FTYPE. For example:
This will make the necessary entries in the registry. For more information, type
ASSOC /?
orFTYPE /?
at the command prompt.If you are planning on providing an installer for your application, simply use the file association feature available in whatever installer framework you choose to use - even the Visual Studio setup project knows how to do this.
To alter file type associations directly from your code, I believe you have to look into
HKEY_CLASSES_ROOT
and find/create a key with the extension you want to bind to (ie ".pdf"). Within this key, the default value is a string containing a reference to another key withinHKEY_CLASSES_ROOT
. Go follow that pointer, expand/create theshell
subkey and add/change your commands here. Look around this area withregedit
to get the fealing of how it looks.I have some C# code in a pet project of mine, which looks up the binding for PDF files and adds an extra option to their context menus. Feel free to have a look.