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

I am trying to find a way to change the default file association for a file extension in Windows 7. I have an application that is used to view .tif files that I want to request from a user if this is not the default viewer for this file type. If they want to do this by default, I want to override the current default viewer. This works great if on a system. When there is another viewer that has been selected by the user, I cannot change the registry key that controls here:

  HKCU \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ FileExts \ .tif \ UserChoice

I looked at the webpage trying to find someone who did this, but no one seems to have the answers. When I try to update the "Progid" value in this, I get the message "Cannot write to the registry key" or "Requested registry access is not allowed." The code is quite simple:

var path = @"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.tif\UserChoice"; var key = Registry.CurrentUser.OpenSubKey(path, true); key.SetValue("Progid", "myprogid..."); 

Is there any special protection on this key that prevents it from being programmatically edited?

+7
source share
4 answers

To be able to write the key UserChoice, you had to pick up the key before writing (for example, this code is not in C # (C ++), but I assume that it can be done the same way).

You can write the key, and your file association code will work!

EDIT: see also Security and Permissions for Registry Keys on MSDN

+1
source

This does not seem to be volatile, but you can remove UserChoice .

0
source
0
source

If you have not figured out any way to do this, try ftype on the command line. Please note that you will need to run them as administrator.

  • Create a script package that uses Assoc and Ftype to establish file associations.
  • Use System.Diagnostics.Process to execute the script package.

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

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

For more information about using this tool, see File Management from the Command Line-Assoc and Ftype .

0
source

All Articles