12

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?

Zombo
  • 1
  • 62
  • 391
  • 407
Jason
  • 2,701
  • 2
  • 25
  • 35
  • Have you tried updating it, for example in a console application, which you run with administrator privileges? – ChristiaanV May 31 '11 at 17:29
  • It is a desktop app, running as myself but without admin privileges. That was my next step. – Jason Jun 01 '11 at 18:30
  • Running as administrator did not help. – Jason Jun 01 '11 at 18:43
  • Are you looking for this function? http://msdn.microsoft.com/en-us/library/windows/desktop/bb776332(v=vs.85).aspx – Simon Mourier Nov 22 '12 at 09:05
  • The question ["Remove a 'Deny' rule (permission) from the 'UserChoice' key in the Registry via C#"](http://stackoverflow.com/q/6108128/1497596) and [this answer](http://stackoverflow.com/a/41290208/1497596) to it might help. – DavidRR Dec 22 '16 at 21:15

4 Answers4

1

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

Nicolas Voron
  • 2,916
  • 1
  • 21
  • 35
0

This link may have more information for you: How do I take ownership of a Registry Key using C# and these: RegOpenKeyEx and RegSetKeySecurity??

Ian R. O'Brien
  • 6,682
  • 9
  • 45
  • 73
0

If you haven't figured out some way to do this try the assoc and ftype command-line tools. Note though that you will need to run them with administrative privileges.

  1. Create a batch script that uses Assoc and Ftype to set the file association.
  2. Use System.Diagnostics.Process to execute the batch script.

You basically have to create a .bat (or .cmd) file with the following lines:

ASSOC .txt=TXTFileWordPad    
FTYPE TXTFileWordPad="%ProgramFiles%\Windows NT\Accessories\wordpad.exe" %%1

For more information on how to use these tool have a look at: Managing Files from the Command Line- Assoc and Ftype.

Panos Rontogiannis
  • 4,154
  • 1
  • 24
  • 29
-1

It seems not changeable but you can delete the UserChoice.

biegleux
  • 13,179
  • 11
  • 45
  • 52
Eben
  • 554
  • 3
  • 11