Adding custom file extension associations

5

I want to add my own custom file extension into the win 7 registry. And I also want to add a right click option "Play with xPlayer" for this extension.

I want to use it in my C# application. How can I do it using the registry?

Barun

Posted 2011-02-08T22:22:28.673

Reputation: 153

The question is offtopic here and belongs to http://superuser.com . The easiest way is to run regedit, check HKEY_CLASSES key for say ".mp3" subkey and see how it works.

– Eugene Mayevski 'Callback – 2011-02-08T22:33:50.830

Sorry I forgot to say I want to use it in my C# application. And actually I have been doing exactly what you said for some hours now. But I can see here different type of pattern. So I need to know the standard way to do this. – None – 2011-02-08T22:37:27.277

Answers

9

MSDN has extensive documentation on how to work with file extensions in the Registry. Start with these articles:

File Types and File Associations

Guidelines for File Associations and Default Programs

Update: You would need to create the following Registry keys and values at a bare minimum (file extension registrations support a LOT of features):

HKEY_CURRENT_USER\Software\Classes\.myext
(Default) = "MyAppExt"

HKEY_CURRENT_USER\Software\Classes\MyAppExt\shell\PlayWithXPlayer
(Default) = "Play with xPlayer"

HKEY_CURRENT_USER\Software\Classes\MyAppExt\shell\PlayWithXPlayer\command
(Default) = ""c:\path to\xplayer.exe" "%1""

Substitute HKEY_LOCAL_MACHINE if you want your file extension to be accessible to all users on the PC, instead of just the user that is running your app/installer.

For example:

using System;
using Microsoft.Win32;

// substitute "HKEY_LOCAL_MACHINE" if needed...
Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\.xpl", "", "xPlayer", RegistryValueKind.String);
Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\xPlayer\\shell\\PlayWithXPlayer", "", "Play with xPlayer", RegistryValueKind.String);
Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\xPlayer\\shell\\PlayWithXPlayer\\command", "", "\"c:\\path to\\xplayer.exe\" \"%1\"", RegistryValueKind.String);

Alternatively:

using System;
using Microsoft.Win32;

// substitute Registry.LocalMachine if needed...
RegistryKey rkRootKey = Registry.CurrentUser;

RegistryKey rk = rkRootKey.CreateSubKey("Software\\Classes\\.xpl");
rk.SetValue("", "xPlayer", RegistryValueKind.String);
rk.Close();

rk = rkRootKey.CreateSubKey("Software\\Classes\\xPlayer\\shell\\PlayWithXPlayer");
rk.SetValue("", "Play with xPlayer", RegistryValueKind.String);

RegistryKey rk2 = rk.CreateSubKey("command");
rk2.SetValue("", "\"c:\\path to\\xplayer.exe\" \"%1\"", RegistryValueKind.String);
rk2.Close();

rk.Close();

When your player app is started, it can look at its command-line parameters, and if it sees a filename passed in, it can play the file as needed.

Remy Lebeau

Posted 2011-02-08T22:22:28.673

Reputation: 243

Thanks for answering. I didn't upvote (nor downvote) because this answer is just links. Links, especially to Microsoft pages, tend to die, leading to a useless answer. If you added some examples here in the answer itself, I'd upvote it. – Chris Moschini – 2015-09-16T18:52:18.390

@ChrisMoschini: done. – Remy Lebeau – 2015-09-17T01:25:15.673