开发者

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

I have been tryi开发者_Python百科ng 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?


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


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


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.


It seems not changeable but you can delete the UserChoice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜