Click here to Skip to main content
16,022,060 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have created a launcher (console all) that opens my customer file with my exe (let's say the custom file extension is ".abcd"). But to open the file a user has to choose open with option and then choose my exe and say open always. Whereas other applications like VLC or Picasa open defined file extensions automatically. Can anyone help to know the tech behind it and how it works so, I can move on the same path and develop something on my own? TIA.

What I have tried:

Just googled about it. Nothing much.
Posted

1 solution

I'm going to assume, for the sake of this answer, that you haven't built an installer for your application. If you had, you could register the extension using the installer. Anyway, assuming that you haven't built an installer, you are going to need to modify the registry to perform the association. Be aware that this means that your application will have to run as administrator to actually make the modification. Something like this should help:
C#
public static void RegisterFileExtension(string extension, string applicationPath)
{
    if (!extension.StartsWith("."))
    {
        extension = "." + extension;
    }

    string applicationName = Path.GetFileNameWithoutExtension(applicationPath);
    string keyName = applicationName + "_file";

    SetSubKey(@"Software\Classes\" + extension, "", keyName);

    SetSubKey(@"Software\Classes\" + extension + @"\shell\open\command", "", "\"" + applicationPath + "\" \"%1\"");
}

private static void SetSubKey(string registryPath, string registryKey, value)
{
    using RegistryKey key = Registry.CurrentUser.CreateSubKey(registryPath);
    key.SetValue(registryKey, value);
}
Obviously, the applicationPath is the path to your executable.
 
Share this answer
 
v2
Comments
charles henington 23-Sep-24 17:38pm    
I'm having issues getting this to compile properly. What is key? Im assuming
RegistryKey key = Registry.LocalMachine;
on key.SetValue(string, string, string); it should be key.SetValue(string, string, RegistryValueKind); if I'm not missing something.
Pete O'Hanlon 24-Sep-24 2:56am    
My apologies for that Charles. In the first draft of my answer, I only had one method. I rewrote that draft to extract the RegistryKey logic to the SetSubKey method, and missed renaming the first entry. I have updated the code with the correct version.
charles henington 24-Sep-24 20:19pm    
my +5
Pete O'Hanlon 25-Sep-24 2:53am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900