C# - 对注册表项“HKEY_CLASSES_ROOT\"的文件关联访问被拒绝 [英] C# - File Assocation Access to the registry key 'HKEY_CLASSES_ROOT\' is denied

查看:56
本文介绍了C# - 对注册表项“HKEY_CLASSES_ROOT\"的文件关联访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个使用注册表设置文件关联的程序(注意,它是 .NET 2.0,所以我必须通过注册表方法).问题是,由于它正在尝试写入 HKCR,因此它可能会遇到管理问题(在其他机器上).是否有更通用的方法来设置文件关联(对于 .NET 2.0),例如使用 HKCU 或其他不需要管理权限的方法.

I currently have a program that sets file association using the registry (Note, it's a .NET 2.0 so I have to go through the registry method). The problem is, since it's trying to write to HKCR, the possibility exists of it running into an administrative problem (on other machines). Is there a more universal way to set file association (for .NET 2.0) such as using HKCU or some other method that doesn't require administrative privileges.

string keyName = "Class";
string keyValue = "Class File";
string apppath = Application.ExecutablePath.ToLower() + " \"%1\"";
RegistryKey key;
key = Registry.ClassesRoot.CreateSubKey(keyName);
key.SetValue("", keyValue);

RegistryKey iconkey;
key = Registry.ClassesRoot.CreateSubKey(keyName);
iconkey = key.CreateSubKey("DefaultIcon");
iconkey.SetValue("", Environment.CurrentDirectory + "\\resources\\musicfile.ico");

key = key.CreateSubKey("shell");
key = key.CreateSubKey("open");
key = key.CreateSubKey("command");
key.SetValue("", apppath);

推荐答案

HKEY_CLASSES_ROOT 是另外两个位置的别名,合并:

HKEY_CLASSES_ROOT is an alias, a merging, of two other locations:

  • HKEY_CURRENT_USER\Software\Classes
  • HKEY_LOCAL_MACHINE\Software\Classes

您必须决定注册文件的原因.文件注册是否应该适用于所有用户?如果是这样,您希望使用 HKEY_CLASSES_ROOT 注册关联(这会将其隐式写入 HKEY_LOCAL_MACHINE.

You have to decide why you're registering the file. Is the file registration supposed to be for all users? If so, you want to register the association with HKEY_CLASSES_ROOT (which will implicitly write it to HKEY_LOCAL_MACHINE.

还是只针对当前用户进行注册?如果是这样,请在 HKEY_CURRENT_USER 中注册您的文件.

Or is the registration meant for only the current user? If so, register your file in HKEY_CURRENT_USER.

请记住,仅仅因为一个用户安装了您的程序并不意味着所有用户都希望拥有它;因此,请考虑该程序的用途.

Keep in mind that just because one user installs your program doesn't mean that all users want to have it; so consider what the program is for.

无论哪种方式,注册文件关联的正确时间是在使用 MSI 安装程序进行安装期间.MSI 安装程序知道如何在安装过程中要求提升 - 如果需要的话.并非所有的软件都必须在 Program Files 中,有些可以在 %APPDATA%(即 Chrome)中安装,无需管理员权限即可安装 - 它仅用于一个用户.

Either way, the proper time to register your file association was during installation with the MSI installer. MSI installers know how to ask for elevation during install - if it's required. Not all software has to go in Program Files, some can go in %APPDATA% (i.e. Chrome) where no admin privileges are required to install - and it's only installed for the one user.

这篇关于C# - 对注册表项“HKEY_CLASSES_ROOT\"的文件关联访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆