Alex Rivera | Logout

C# - File Assocation Access to the registry key 'HKEY_CLASSES_ROOT\' is denied

Asked 2011-01-05T19:48:19.730
9

I currently have a program that sets file association using the registry (Note, it's a .NET 2.0 so I have to go through the registry method). The problem is, since it's trying to write to HKCR, the possibility exists of it running into an administrative problem (on other machines). Is there a more universal way to set file association (for .NET 2.0) such as using HKCU or some other method that doesn't require administrative privileges.

string keyName = "Class";
string keyValue = "Class File";
string apppath = Application.ExecutablePath.ToLower() + " \"%1\"";
RegistryKey key;
key = Registry.ClassesRoot.CreateSubKey(keyName);
key.SetValue("", keyValue);

RegistryKey iconkey;
key = Registry.ClassesRoot.CreateSubKey(keyName);
iconkey = key.CreateSubKey("DefaultIcon");
iconkey.SetValue("", Environment.CurrentDirectory + "\\resources\\musicfile.ico");

key = key.CreateSubKey("shell");
key = key.CreateSubKey("open");
key = key.CreateSubKey("command");
key.SetValue("", apppath);
Edit
Report

1 Answer

6

You can use user-specific file associations in situations where you do not have access to HKLM.

Alternatively you can manifest your app to require administrator rights.

More normally file associations are set during application install where admin rights are normally available.

answered 2011-01-05T19:53:27.240

Your Answer