Windows 7 文件扩展名关联

发布于 2024-09-17 11:43:07 字数 1019 浏览 6 评论 0原文

我特指 Windows 7。

我有将某个扩展与我的应用程序关联起来的代码,如 webJose 在下一页上所建议的那样: 哪些注册表项负责文件扩展关联? (但是,我按照建议正确写入 HKEY_CURRENT_USER\Software\Classes 而不是 HKEY_CLASSES_ROOT)

上述内容最初有效,或者如果没有与扩展关联的其他程序。但是,在使用 Windows 7 内置的“选择默认程序...”(位于“打开方式”下的文件右键单击上下文菜单下)后,它会将扩展名与您选择的任何新程序重新关联。

此时发生的情况是“HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\\UserChoice”被系统更改,因此新选择的程序接管。

运行上面的代码,重新获得对扩展的控制将不起作用。重新获得控制权的唯一方法是:

  1. 编辑 UserChoice -> Progid 值,这是不允许的(无论是通过编程方式还是使用 regedit.exe - 访问被拒绝)。
  2. 或者删除 UserChoice 值并确保您的应用程序是 \OpenWithList 下 MRUList 值中的第一个(这可以使用 regedit.exe 实现,但不能以编程方式实现)

我的问题是:有没有办法以编程方式实现此目的?在与另一个程序关联后,可以更改哪些注册表值来重新获得对扩展的控制?

我知道,如果用户通过资源管理器将关联的应用程序设置为扩展,则可能会以相同的方式再次将扩展重新关联到不同的应用程序,这似乎很明显。

然而问题是我的应用程序中有一个按钮,它使用上述代码来检查扩展与我的应用程序的关联。不幸的是,在上述情况下,我的应用程序显示一条消息,确认扩展程序已成功关联,但实际上并未成功关联!那么有办法解决这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ㄖ落Θ余辉 2024-09-24 11:43:07

删除 UserChoice 应该将默认程序恢复为标准文件关联键(以 HKCU 中的 ProgID 开头)。除非您也可以删除 OpenWithList,这会带来极大的偏见。

编辑:
查看注册表密钥安全和访问权限 MSDN,特别是 RegSetKeySecurity 函数。请记住,您需要先授予自己对该密钥的管理控制权,然后才能将其删除。

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.

寻找我们的幸福 2024-09-24 11:43:07

关于 Window 7 中的文件关联,出现了一个新的“问题”。

这是其中之一:你必须为自己的权利而战。

假设你喜欢跑步,

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

你就会被拒绝访问。
当您检查 Regedit“UserChoice”中密钥的安全设置时,您会看到有一个为您设置的设置窗口,用于拒绝当前用户的“设置”。好吧,您可以在 regedit 中更改/删除此设置,现在您可以删除 UserChoice。
然而,对于程序员/脚本编写者来说,这种设置有点麻烦,因为现在有真正的工具可以在注册表中设置 ACL。然而,这里有一些解决方法,允许删除访问被拒绝的密钥(当然,这仅在您有权更改权限的情况下有效):

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

总结一下,这里的主要内容是REG RESTORE + 只包含空密钥的注册表配置单元文件。
Regedit 中,这相当于导入一个空的注册表结构文件(注意:这是一个 hive 文件,而不是 *.reg 文件)。

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).

北城挽邺 2024-09-24 11:43:07

我的解决方案不是尝试删除 UserChoice 键(只有管理员可以这样做),而是删除 ProgId 指向的键。如果用户过去做出过选择,则 ProgId 的值类似于 Applications\*.exe。非管理员可以批量删除密钥:

REG DELETE HKCR\Applications\*.exe /f

这可能有点黑客,但对我有用。

My solution was not to try to delete the UserChoice key (only administrators can do it), but to delete the key the ProgId is pointing to. If the user made a choice in the past the ProgId has a value like Applications\*.exe. The non-admin can delete the key in a batch:

REG DELETE HKCR\Applications\*.exe /f

This might be a bit of a hack, but worked for me.

花期渐远 2024-09-24 11:43:07

同样在 Windows 10 上,此命令无法工作:

Reg.exe delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.m4v" /f

解决方案是通过在 TrustedInstaller 下运行 BAT 文件来删除 SID 密钥(使用 NSudo、PowerRun 等):

for /f "delims=\ tokens=2" %%A in ('reg query hku ^| findstr /i "S-1-5-21-" ^| findstr /v /i "_Classes"') do set SID=%%A
Reg.exe delete "HKU\%SID%\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.m4v" /f

PS:感谢 @SecurityAndPrivacyGuru用于共享SID检测命令

Also on Windows 10, this command can't work:

Reg.exe delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.m4v" /f

The solution is to delete the SID key by running a BAT file under TrustedInstaller (with NSudo, PowerRun, etc):

for /f "delims=\ tokens=2" %%A in ('reg query hku ^| findstr /i "S-1-5-21-" ^| findstr /v /i "_Classes"') do set SID=%%A
Reg.exe delete "HKU\%SID%\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.m4v" /f

P.S.: Thanks to @SecurityAndPrivacyGuru for sharing the SID detection command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文