Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows 7 file extension association

I am referring specifically to windows 7.

I have code that associates a certain extension with my application as proposed by webJose on the following page: What registry keys are responsible for file extension association? (However i correctly write to HKEY_CURRENT_USER\Software\Classes instead of HKEY_CLASSES_ROOT as suggested)

The above works initially, or if there are no other programs associated with the extension. However after using the Windows 7 built-in "Choose default program..." (found under the file-right-click context menu under "Open with") it re-associates the extension with whatever new program you choose.

What happens at this point is that "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\\UserChoice" is changed by the system, and so the newly selected program takes over.

Running the above code, to regain control over the extension will not work. The only way to regain control, is by either:

  1. Editing the UserChoice -> Progid value, which is not allowed (neither programmatically nor using regedit.exe - access denied).
  2. Or deleting the UserChoice value and making sure your application is the first in the MRUList value under \OpenWithList (this can be achieved using regedit.exe but not programmatically)

My question is: Is there a way to achieve this programmatically? What registry values can be changed to regain control of an extension, after is associated with another program?

I know it might seem obvious that if a user through explorer sets the associated application to an extension, that it would be expected to do it the same way again to re-associate the extension to a different application.

The problem however is I have a button in my application that uses the above mentioned code to check for extension association with my application. Unfortunately with the above situation, my application displays a message confirming that the extension is already successfully associated when its not! So is there a way around this?

like image 801
Tamer Avatar asked Sep 01 '10 17:09

Tamer


People also ask

How do I change file associations back to default?

Step 1: Right-click on a file of the type you wish to change the association for. Step 2: Select Open With from the resulting menu. Step 3: Windows will then offer you an app or a list of apps that can act as the default for that file type.

How do I associate a file extension to a program?

Open the Control Panel. In the Control Panel, click the Default Programs option. Click the Associate a file type or protocol with a program option. In the Default apps window, scroll to the bottom and select Choose defaults by file type.


2 Answers

Deleting UserChoice should revert the default program to the standard file association keys (which starts with the ProgID in HKCU). Barring that you could also delete OpenWithList, which would be reverting with extreme prejudice.

Edit: Check out Registry Key Security and Access Rights on MSDN, particularly the RegSetKeySecurity function. Remember that you'll need to grant yourself administrative control to the key before you can delete it.

like image 104
Factor Mystic Avatar answered Oct 05 '22 23:10

Factor Mystic


Well regarding file assoc in Window 7 a new 'problem' araised.

It's one of this: You've to fight for your rights.

Assuming you like to run

REG.exe DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.mov\UserChoice" /f /va

You'll get ACCESS DENYED. When you check security setting of the key in Regedit 'UserChoice' you'll see that there's a setting windows made for you, to deny 'set' for the current user. Well you maybe change/delete this setting in regedit and now you can delete UserChoice. However for programmer/scripters that setting is a little bitchy since there are now real tools to set ACLs in the registry. However here some workaround that at allows to delete keys with ACCESS DENYED (of course this only works incase you've the right to change permissions):

ResetMovAssoc.cmd

::create 'empty.hiv' 
REG ADD "HKCU\emptyKey" /f
REG SAVE "HKCU\emptyKey" empty.hiv /y
@REG DELETE "HKCU\emptyKey" /f >nul
::^-note you can add @[...] >nul to the other entries as well to run them quite

:: Delete Reg key by replacing it with an empty hiv
REG RESTORE "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.mov" empty.hiv
del empty.hiv

To summarize this the major thing here is REG RESTORE + Registry hive file containing just and empty key. In Regedit that'll equivalent to Import' with a just an empty registry structure file (Note: that's a hive file and not a *.reg file).

like image 41
Nadu Avatar answered Oct 06 '22 01:10

Nadu