Registry Configuration for Launching Programs by File Extensions

Associating a specific program with a particular file type through registry configurations involves defining entries in the Windows Registry. This process enables the operating system to discern the appropriate program to utilize when a specific file is accessed, either through a double-click or alternative means.

Here's a detailed breakdown.

  1. File Extension Association: Each file type is linked to a corresponding file extension (e.g., .txt for text files, .jpg for images), and the Windows Registry maintains a comprehensive list of these associations.
  2. Registry Keys: The Windows Registry stores information about file extension associations under keys associated with file types. For instance, the registry key for .txt files may be located under HKEY_CLASSES_ROOT\.txt.
  3. ProgID (Program Identifier): Every file type is assigned a ProgID, a unique identifier connected to a program equipped to handle that specific file type.
  4. Default Program: The ProgID directs to a series of registry keys, including one that designates the default program linked to the file type.
  5. Default Icon: Typically, within the registry keys, there exists an entry for the default icon associated with the program. This icon is presented when viewing files of that type.
  6. Shell Commands: Additional entries in the registry detail shell commands, such as "open," specifying the actions to be executed when a file of that type is opened.
  7. Modification via Code: Developers can programmatically adjust these registry entries to associate their applications with specific file types. This is frequently performed during the installation of an application.
  8. User Experience: When a user initiates a double-click on a file with a registered extension, the operating system references the registry to identify the associated program, launching it to manage the file.

Use the following code

private void SetRegisterExtension()
{
    string fileExtension = ".ariarchfle";  // Replace with your desired file extension
    string iconFilePath = "C:\\ProgramData\\Testing\\Imagesicons\\icons_32x32.ico";  // Replace with the path to your icon file
    string executablePath = "C:\\ProgramData\\Testing\\Executable\\WpfAppTest.exe";  // Replace with the path to your executable
    RegisterFileExtension(fileExtension, iconFilePath, executablePath);
}

private void RegisterFileExtension(string fileExtension, string iconFilePath, string executablePath)
{
    try
    {
        // Open the key for the file extension
        using (RegistryKey extensionKey = Registry.ClassesRoot.CreateSubKey(fileExtension))
        {
            if (extensionKey != null)
            {
                // Set the default value to the ProgID (file type identifier)
                extensionKey.SetValue(null, "CustomType");

                // Create a new key for the ProgID
                using (RegistryKey progIdKey = Registry.ClassesRoot.CreateSubKey("CustomType"))
                {
                    if (progIdKey != null)
                    {
                        // Set the default value for the ProgID key to the description of your file type
                        progIdKey.SetValue(null, "CustomTypeId");

                        // Create a new key for the DefaultIcon
                        using (RegistryKey iconKey = progIdKey.CreateSubKey("DefaultIcon"))
                        {
                            if (iconKey != null)
                            {
                                // Set the default value of the DefaultIcon key to the path of your icon file
                                iconKey.SetValue(null, iconFilePath);

                                // Create a new key for the shell open command
                                using (RegistryKey commandKey = progIdKey.CreateSubKey(@"shell\open\command"))
                                {
                                    if (commandKey != null)
                                    {
                                        // Set the default value of the command key to the path of your executable
                                        commandKey.SetValue(null, executablePath);
                                        Console.WriteLine($"File extension '{fileExtension}' registered with icon and executable.");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }
}

Source Code Repository: https://github.com/OmatrixTech/SampleProject


Similar Articles