Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to programmatically set the "UserChoice" registry key to take over a file type association?

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?

like image 325
Jason Avatar asked May 31 '11 17:05

Jason


People also ask

Where in the registry are file associations stored?

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.

Which registry key contains the user profile for the user who is currently logged on to the computer?

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.

Where in the registry is software is set to run once at start up?

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce.


1 Answers

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

like image 173
Nicolas Voron Avatar answered Sep 18 '22 13:09

Nicolas Voron