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?
File associations are stored in both HKLM\SOFTWARE\Classes and HKCU\SOFTWARE\Classes; you can see a merged view of the data under HKEY_CLASSES_ROOT.
For that, you need to use Command Prompt. Press Start, type cmd and it'll find Command Prompt. Right click the entry and click Run as administrator. Type assoc, press Enter, and it'll bring up all file types and their associations.
The open with associations are all stored in HKEY_CLASSES_ROOT .
You can use the keyboard shortcut Windows key + E to open Explorer if you're unsure where the file is. Right-click the file you want to change the file association and select Properties in the drop-down menu. In the file Properties, click the Change button next to the "Opens with" option.
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:
FTYPE SMCFile="C:\some_path\SMCProgram.exe" -some_option %1 %*
ASSOC .smc=SMCFile
This will make the necessary entries in the registry. For more information, type ASSOC /?
or FTYPE /?
at the command prompt.
Use the Registry
class in Microsoft.Win32
.
Specifically, you want the ClassesRoot
property of Registry
to access the HKEY_CLASSES_ROOT
key (cf. Understanding MS Windows File Associations and HKEY_CLASSES_ROOT: Core Services).
using Microsoft.Win32;
Registry
.ClassesRoot
.CreateSubKey(".smc")
.SetValue("", "SMC", RegistryValueKind.String);
Registry
.ClassesRoot
.CreateSubKey("SMC\shell\open\command")
.SetValue("", "SMCProcessor \"%1\"", RegistryValueKind.String);
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
Process.Start("SMCProcessor blblabla.smc");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With