在C#中,如何删除“资源管理器文件关联”覆盖“注册表类”文件关联?
问题描述:
如何以编程方式删除Explorer创建的所有“文件关联”,以便HKCR下的文件关联正常工作?在C#中,如何删除“资源管理器文件关联”覆盖“注册表类”文件关联?
例如,我想删除相应用户在资源管理器中单击“打开...”的注册表项。例如:
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt
或其他文件扩展名,如.py或.pl。
我写了一些C#代码来做到这一点...但是它会抛出一个异常。这里就是我想要做的事:
ExplorerOverrideDelete(".txt");
// Throws Exception:
// System.UnauthorizedAccessException: Cannot write to the registry key.
这里的功能:
public static void ExplorerOverrideDelete (string ext)
{
if (!ext.StartsWith(".")) {
ext = "." + ext;
}
var hivereg = GetHive("HKCU");
var key = hivereg.OpenSubKey(
@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\"
+ ext);
if (key == null)
return;
var keyroot = hivereg.OpenSubKey(
@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts");
if (keyroot == null)
return;
keyroot.DeleteSubKeyTree(ext);
key.Dispose();
keyroot.Dispose();
hivereg.Dispose();
}
private static RegistryKey GetHive(string hive) {
switch(hive.ToUpper()) {
case "HKCU": return Registry.CurrentUser;
case "HKLM": return Registry.LocalMachine;
case "HKCR": return Microsoft.Win32.Registry.ClassesRoot;
}
return null;
}
一些更多的信息:
PS C:\Users\john\Documents> get-acl HKCU:Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts | fl
Path : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileE
xts
Owner : DESKTOP-D3USQOT\john
Group : DESKTOP-D3USQOT\None
Access : DESKTOP-D3USQOT\john Allow FullControl
NT AUTHORITY\SYSTEM Allow FullControl
BUILTIN\Administrators Allow FullControl
NT AUTHORITY\RESTRICTED Allow ReadKey
APPLICATION PACKAGE AUTHORITY\ALL APPLICATION PACKAGES Allow R
PS C:\Users\john\Documents> get-acl HKCU:Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt | fl
Path : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileE
xts\.txt
Owner : DESKTOP-D3USQOT\john
Group : DESKTOP-D3USQOT\None
Access : DESKTOP-D3USQOT\john Allow FullControl
NT AUTHORITY\SYSTEM Allow FullControl
BUILTIN\Administrators Allow FullControl
NT AUTHORITY\RESTRICTED Allow ReadKey
APPLICATION PACKAGE AUTHORITY\ALL APPLICATION PACKAGES
答
下面是对我工作的代码。我仍然不完全明白为什么我需要以管理员身份运行此操作,因为当前用户的ACL具有完全控制权。如果有人对此有任何想法,请在下面给我留言。谢谢。
public static void ExplorerOverrideDelete (string ext)
{
if (!IsAdministrator()) {
MessageBox.Show(
"ExplorerOverrideDelete(): Rerun program as Admin",
"Access Denied"
}
if (!ext.StartsWith(".")) {
ext = "." + ext;
}
var keyroot = Registry.CurrentUser.OpenSubKey(
@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts",
RegistryKeyPermissionCheck.ReadWriteSubTree);
if (keyroot == null)
return;
keyroot.GetAccessControl(System.Security.AccessControl.AccessControlSections.All);
// /keyroot.GetAccessControl(
// System.Security.AccessControl.AccessControlSections.All
// & ~System.Security.AccessControl.AccessControlSections.Audit);
try {
keyroot.DeleteSubKeyTree(ext);
}
catch(Exception e) {
keyroot.Close();
hivereg.Close();
MessageBox.Show(e.Message);
}
keyroot.Close();
hivereg.Close();
}
public static bool IsAdministrator()
{
System.Security.Principal.WindowsIdentity identity
= System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal
= new System.Security.Principal.WindowsPrincipal(identity);
return principal.IsInRole(
System.Security.Principal.WindowsBuiltInRole.Administrator
);
}
“UnauthorizedAccessException”非常清楚。尝试以管理员身份运行程序。 – Blorgbeard
我的理解是你不需要管理员来修改HKCU。 HKLM是需要管理员权限的配置单元。另外,我已经尝试过了。 –