Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change filetype association in the registry?

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?

like image 204
Sergio Tapia Avatar asked Jul 04 '09 20:07

Sergio Tapia


People also ask

Where are file associations in the registry?

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.

Where are file type associations in Windows 10 registry?

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.

Where are registry key associations for file extensions located in the registry?

The open with associations are all stored in HKEY_CLASSES_ROOT .

Where can file associations be changed?

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.


2 Answers

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.

like image 86
ars Avatar answered Sep 23 '22 03:09

ars


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");
like image 33
jason Avatar answered Sep 21 '22 03:09

jason