DEV Community


Posted on

Registry Configuration for Launching Programs by File Extensions

Title: C# Registry Configuration for Launching Programs by File Extensions

Adding the ability to launch programs based on file extensions is a powerful feature that enhances the user experience by opening specific applications for specific files. In this post, we will explore how to configure the Windows registry using C# to associate file extensions with programs and provide a sample code to demonstrate the implementation.

Step 1: Understand the Registry
The Windows registry is a hierarchical database that contains settings and configurations for various software and hardware components. To associate file extensions with programs, we need to modify specific registry keys.

Step 2: Add the Required Namespace
To work with the Windows registry in C#, we need to include the "Microsoft.Win32" namespace. This provides us with classes and methods that allow us to access and modify registry keys.

Step 3: Retrieve and Modify Registry Keys
To associate a file extension with a program, we need to find the appropriate registry key and modify its value accordingly. Here's an example code snippet to accomplish this:

using Microsoft.Win32;


// File extension and program path variables for demonstration
string fileExtension = ".txt";
string programPath = "C:\\Program Files\\Notepad++\\notepad++.exe";

// Retrieve the registry key for file associations
RegistryKey fileAssociationKey = Registry.ClassesRoot.OpenSubKey(fileExtension, true);

// If the key doesn't exist, create it
if (fileAssociationKey == null)
    fileAssociationKey = Registry.ClassesRoot.CreateSubKey(fileExtension);

// Set the value for the key to the program path
fileAssociationKey.SetValue("", "CustomProgram");

// Create a subkey to hold the program information
RegistryKey programKey = Registry.ClassesRoot.CreateSubKey("CustomProgram");
programKey.SetValue("", "Custom Program");
programKey.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + programPath + "\" \"%1\"");
Enter fullscreen mode Exit fullscreen mode


  1. We first define the file extension (e.g., ".txt") and the path to the program we want to associate with it.
  2. We then retrieve the appropriate registry key associated with the file extension, using "Registry.ClassesRoot.OpenSubKey()".
  3. If the key does not exist, we create it using "Registry.ClassesRoot.CreateSubKey()".
  4. We set the value of the key to a custom program name, in this case, "CustomProgram".
  5. We create a subkey named "CustomProgram" to hold the program information.
  6. Within the "CustomProgram" subkey, we create a "shell" subkey, followed by an "open" subkey, and finally a "command" subkey.
  7. We set the value of the "command" subkey to the program path, along with "%1" to indicate that it should open the file associated with the extension.

This code associates the ".txt" file extension with a custom program path, allowing the program to launch whenever a file with this extension is double-clicked or opened.

Configuring the Windows registry using C# provides developers with a seamless method to associate file extensions with specific programs, enhancing user experience. By following the steps outlined above, you can add this functionality to your own applications. Remember to handle exceptions appropriately and ensure that you have administrator privileges when modifying the registry.

Top comments (0)