I have been trying to find a way to change the default file association for a specific file extension in windows 7. I have an app that is used to view .tif files that I want to prompt th user if its not the default viewer for that file type. If they choose to make it the default, I want to override the current default viewer. This works fine if there are no other viewers installed on the system. When there is another viewer that has been selected by the user, I cannot change the registry key that controls that here:
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.tif\UserChoice
I have scoured the web trying to find someone that has done this, but nobody seems to have any answers. When I try to update the "Progid" value within this key, I get a "Cannot write to the registry key" or "Requested registry access is not allowed" errors. The code is simple enough:
var path = @"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.tif\UserChoice";
var key = Registry.CurrentUser.OpenSubKey(path, true);
key.SetValue("Progid", "myprogid...");
Is there some special protection on this key that prevents it from being programmatically edited?
File associations are stored in both HKLM\SOFTWARE\Classes and HKCU\SOFTWARE\Classes; you can see a merged view of the data under HKEY_CLASSES_ROOT.
This key is sometimes abbreviated as HKCU. Contains all the actively loaded user profiles on the computer. HKEY_CURRENT_USER is a subkey of HKEY_USERS. HKEY_USERS is sometimes abbreviated as HKU.
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce.
To be able to write to the key UserChoice you needed to take ownership of the key before writing to it (like this code - not in c# (C++), but I assume it can be done the same way).
You will be able to write to the key, and your file association code will works !
EDIT : See also Registry Key Security and Access Rights on MSDN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With