C# - 文件关联访问注册表项“HKEY_CLASSES_ROOT\”被拒绝

发布于 2024-10-10 09:05:20 字数 722 浏览 0 评论 0原文

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

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

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

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

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

发布评论

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

评论(2

风筝在阴天搁浅。 2024-10-17 09:05:20

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

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

您必须决定为什么要这样做正在注册文件。文件注册是否适用于所有用户?如果是这样,您想注册与 HKEY_CLASSES_ROOT 的关联(这将隐式地将其写入 HKEY_LOCAL_MACHINE

或者注册仅适用于当前用户?如果是这样,请注册您的 适当

请记住,仅仅因为一个用户安装了您的程序并不意味着所有用户都想要拥有它;因此无论哪种方式,都要考虑注册该程序的

时间 。您的文件关联是在使用 MSI 安装程序进行安装时进行的。安装程序知道如何在安装过程中要求提升权限 - 如果需要,并非所有软件都必须放入 Program Files 中,有些可以放入 中。 %APPDATA%(即 Chrome),无需管理员权限即可安装 - 并且仅为一位用户安装。

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

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

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.

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.

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.

逆夏时光 2024-10-17 09:05:20

在您无权访问 HKLM 的情况下,您可以使用用户特定的文件关联

或者,您可以将您的应用程序表明为需要管理员权限。

更常见的是,文件关联是在应用程序安装期间设置的,其中管理员权限通常可用。

You can use user-specific file associations in situations where you do not have access to HKLM.

Alternatively you can manifest your app to require administrator rights.

More normally file associations are set during application install where admin rights are normally available.

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