c# hkey_local_machine不允许所请求的注册表访问权
时间: 2023-12-03 14:04:47 浏览: 29
这个错误通常是由于权限不足引起的。当你尝试访问 HKEY_LOCAL_MACHINE 注册表时,你需要以管理员权限运行你的程序,否则你将无法访问该项。
你可以尝试以下步骤来解决该问题:
1. 以管理员身份运行你的程序。
2. 如果你已经在以管理员身份运行你的程序,你可以尝试将你的程序的权限设置为允许访问 HKEY_LOCAL_MACHINE。
3. 如果你仍然无法访问 HKEY_LOCAL_MACHINE,请检查你的操作系统和你的程序是否都是 64 位的。如果是,则需要以 64 位的方式打开你的程序,否则你将无法访问 HKEY_LOCAL_MACHINE。
希望这些步骤能帮助你解决该问题。
相关问题
注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\含义
注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class是Windows操作系统中的一个重要注册表路径,它包含了系统的设备驱动程序信息。该路径下的每个子项对应一个设备类别,如磁盘驱动器、USB设备等。每个设备类别下又包含了该类别的设备驱动程序相关的信息和设置。
通过该注册表路径的相关设置,操作系统可以识别和管理各类设备,并执行相应的驱动程序以提供相应的功能和服务。例如,控制类别下的LowerFilters子键可能包含了设备驱动程序的过滤器,用于对设备进行附加功能或修改设备的行为。而在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB下面的子项则记录了系统中所有USB设备的信息。
总之,注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class存储了系统中各类设备的驱动程序信息和设置,对于系统的设备管理和功能提供起着重要的作用。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [让SSD硬盘提速 Flashpoint2009beta4](https://download.csdn.net/download/vichu/4173203)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [C#在Win 7/8/10 OS下删除HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB子项](https://blog.csdn.net/zbbfb2001/article/details/80059107)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [注册表中HKEY_LOCAL_MACHINE\SYSTEM\下的CurrentControlSet ControlSet001 ControlSet002 的区别](https://blog.csdn.net/babytiger/article/details/130373023)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
C#窗口程序 将当前文件夹下的所有文件和文件夹打包,成abc.exe;abc.exe执行时具有如下功能: 1. 读取注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值保存为字符串strShell,strShell的字符串格式举例为"C:\2.4.7460.12546\autostart.exe doctorold.exe"; 2. 将strShell识别字符串中路径的子字符串保存为strPath; 3. 读取当前目录下的DoctorOld.exe文件版本保存为字符串strNewVer; 4. 新建"C:\"+strNewVer文件夹,其中包括判断是否存在,进行提示“版本相同是否进行强制更新”,如果强制更新则将旧文件夹进行重命名,重命名规则为在文件夹后加当前时间,将新文件夹路径给strPath赋值; 5. 复制当前文件夹所有文件至"C:\"+strNewVer文件夹; 6. 复制strPath目录下的asd.txt文件至"C:\"+strNewVer文件夹直接覆盖; 7. 查找strShell中是否含有“Doctorold.exe”、"Q.exe"、"W.exe",分别存布尔型变量; 8. 如果含有“Doctorold.exe”则修改注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值为"C:\"+strNewVer+"autostart.exe doctorold.exe"; 9. 如果含有“Q.exe”则修改注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值为"C:\"+strNewVer+"autostart.exe Q.exe"; 10. 如果含有“W.exe”则修改注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值为"C:\"+strNewVer+"autostart.exe W.exe";
以下是C#窗口程序的代码实现:
```csharp
using System;
using System.IO;
using System.Diagnostics;
using Microsoft.Win32;
namespace FilePacker
{
class Program
{
static void Main(string[] args)
{
// 1. 读取注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值保存为字符串strShell
string strShell = "";
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"System\Setup");
if (key != null)
{
strShell = key.GetValue("shell") as string;
key.Close();
}
// 2. 将strShell识别字符串中路径的子字符串保存为strPath
string[] shellItems = strShell.Split(' ');
string strPath = "";
foreach (var item in shellItems)
{
if (item.EndsWith(".exe"))
{
strPath = Path.GetDirectoryName(item);
break;
}
}
// 3. 读取当前目录下的DoctorOld.exe文件版本保存为字符串strNewVer
string doctorOldPath = Path.Combine(Environment.CurrentDirectory, "DoctorOld.exe");
string strNewVer = FileVersionInfo.GetVersionInfo(doctorOldPath).FileVersion;
// 4. 新建"C:\"+strNewVer文件夹,其中包括判断是否存在,进行提示“版本相同是否进行强制更新”,如果强制更新则将旧文件夹进行重命名,重命名规则为在文件夹后加当前时间,将新文件夹路径给strPath赋值
string newPath = Path.Combine("C:\\", strNewVer);
if (Directory.Exists(newPath))
{
Console.WriteLine("版本相同是否进行强制更新?(y/n)");
if (Console.ReadLine() == "y")
{
string renamedPath = newPath + DateTime.Now.ToString("yyyyMMddHHmmss");
Directory.Move(newPath, renamedPath);
}
else
{
return;
}
}
Directory.CreateDirectory(newPath);
strPath = newPath;
// 5. 复制当前文件夹所有文件至"C:\"+strNewVer文件夹
foreach (string file in Directory.GetFiles(Environment.CurrentDirectory))
{
File.Copy(file, Path.Combine(newPath, Path.GetFileName(file)), true);
}
foreach (string dir in Directory.GetDirectories(Environment.CurrentDirectory))
{
string newDir = Path.Combine(newPath, Path.GetFileName(dir));
Directory.CreateDirectory(newDir);
foreach (string file in Directory.GetFiles(dir))
{
File.Copy(file, Path.Combine(newDir, Path.GetFileName(file)), true);
}
}
// 6. 复制strPath目录下的asd.txt文件至"C:\"+strNewVer文件夹直接覆盖
string asdPath = Path.Combine(strPath, "asd.txt");
if (File.Exists(asdPath))
{
File.Copy(asdPath, Path.Combine(newPath, "asd.txt"), true);
}
// 7. 查找strShell中是否含有“Doctorold.exe”、"Q.exe"、"W.exe",分别存布尔型变量
bool hasDoctorOld = false;
bool hasQ = false;
bool hasW = false;
foreach (var item in shellItems)
{
if (item.EndsWith("Doctorold.exe"))
{
hasDoctorOld = true;
}
if (item.EndsWith("Q.exe"))
{
hasQ = true;
}
if (item.EndsWith("W.exe"))
{
hasW = true;
}
}
// 8. 如果含有“Doctorold.exe”则修改注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值为"C:\"+strNewVer+"autostart.exe doctorold.exe";
if (hasDoctorOld)
{
strShell = strShell.Replace("Doctorold.exe", Path.Combine(newPath, "autostart.exe doctorold.exe"));
}
// 9. 如果含有“Q.exe”则修改注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值为"C:\"+strNewVer+"autostart.exe Q.exe";
if (hasQ)
{
strShell = strShell.Replace("Q.exe", Path.Combine(newPath, "autostart.exe Q.exe"));
}
// 10. 如果含有“W.exe”则修改注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值为"C:\"+strNewVer+"autostart.exe W.exe";
if (hasW)
{
strShell = strShell.Replace("W.exe", Path.Combine(newPath, "autostart.exe W.exe"));
}
// 更新注册表
key = Registry.LocalMachine.OpenSubKey(@"System\Setup", true);
if (key != null)
{
key.SetValue("shell", strShell);
key.Close();
}
Console.WriteLine("打包完成,按任意键退出...");
Console.ReadKey();
}
}
}
```