vb.net 不允许请求的注册表访问 #2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14411850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Requested registry access is not allowed #2
提问by Tyler Lambe
I've recently been writing an application that needs access to the following registry key :
我最近一直在编写一个需要访问以下注册表项的应用程序:
HKLM\SOFTWARE\Wow6432Node\Classes\CLSID
HKLM\SOFTWARE\Wow6432Node\Classes\CLSID
For some odd reason, I'm not allowed access to this key on any system I've tested on. I'm using administrative rights and everything in between to attempt to accomplish this. I've searched through the first 5 Google result pages and still turned up empty.
出于某种奇怪的原因,我不允许在我测试过的任何系统上访问此密钥。我正在使用管理权限和介于两者之间的所有内容来尝试完成此操作。我已经搜索了前 5 个 Google 结果页面,但仍然是空的。
Things to note :
1. The keys I'm attemping to delete may or may not contain subkeys, I've thoroughly tested "DeleteSubKey" and "DeleteSubKeyTree".
2. I've tried OpenSubKey("Key",True), both false and true values still disallowed me access.
3. I'm also not allowed access to the non-64 bit location (HKLM\Software\Classes\CLSID).
4. I've tested this on XP, 7 & 8. XP didn't give me an issue.
5. The exact error I'm receiving can be found below :
注意事项:
1. 我试图删除的键可能包含也可能不包含子键,我已经彻底测试了“DeleteSubKey”和“DeleteSubKeyTree”。
2. 我试过 OpenSubKey("Key",True),false 和 true 值仍然不允许我访问。
3. 我也不允许访问非 64 位位置 (HKLM\Software\Classes\CLSID)。
4. 我已经在 XP、7 和 8 上测试过了。XP没有给我带来问题。
5.我收到的确切错误可以在下面找到:
Requested registry access is not allowed. at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
不允许请求的注册表访问。在 Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
PLEASE help me..this has been killing me for a few days now. Any help is greatly appreciated.
请帮助我..这已经让我丧了几天了。任何帮助是极大的赞赏。
回答by DareDevil
Microsoft.Win32.RegistryKey m_RegEntry = Microsoft.Win32.Registry.LocalMachine;
m_RegEntry = m_RegEntry.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\{4D36E96D-E325-11CE-BFC1-08002BE10318}");
//string
int i = 0;
string[] m_szModemEntries = m_RegEntry.GetSubKeyNames();
This return many entries connected Devices to COM port . and an entry of "Properties" that we don't need to access.
这将返回许多连接 Devices 到 COM 端口的条目。以及我们不需要访问的“属性”条目。
below I am attaching a simple code to work with it.
下面我附上一个简单的代码来使用它。
string[] m_szModem;
Microsoft.Win32.RegistryKey m_RegEntry = Microsoft.Win32.Registry.LocalMachine;
m_RegEntry = m_RegEntry.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\{4D36E96D-E325-11CE-BFC1-08002BE10318}");
//string
int i = 0;
string[] m_szModemEntries = m_RegEntry.GetSubKeyNames();
m_szModem = new string[m_szModemEntries.Length];
string m_szModemPort = null;
string m_szModemName = null;
foreach (string m_szModemEntry in m_szModemEntries)
{
if (!IsNumber(m_szModemEntry))
{
}
else
{
m_RegEntry.Close();
m_RegEntry = Microsoft.Win32.Registry.LocalMachine;
string m_szKeyName = @"SYSTEM\CurrentControlSet\Control\Class\{4D36E96D-E325-11CE-BFC1-08002BE10318}\" + m_szModemEntry;
m_RegEntry = m_RegEntry.OpenSubKey(m_szKeyName);
m_szModemPort = m_RegEntry.GetValue("AttachedTo").ToString();
m_szModemName = m_RegEntry.GetValue("Model").ToString();
if (m_szModemName.Contains("<device name>"))
{
CommPort = m_szModemPort;
lbldevicename.Text = "Device connected!";
lbldevicename.ForeColor = Color.Green;
cmdProgram.Enabled = true;
DeviceConnected = true;
break;
}
CommPort = "";
cmdProgram.Enabled = false;
lbldevicename.Text = "Device not connected!";
lbldevicename.ForeColor = Color.Red;
DeviceConnected = false;
}
}
//and IsNumber Function
//和IsNumber函数
public Boolean IsNumber(String s)
{
foreach (Char ch in s)
{
if (!Char.IsDigit(ch)) return false;
}
return true;
}
回答by roy.me
Here's a solution I've found which works without Admin rights: just leave out the 'true' paramter, and it will read it all. Worked for me!
这是我发现的一个无需管理员权限即可工作的解决方案:只需省略“真实”参数,它就会读取所有内容。对我来说有效!
回答by xpda
You may need to modify the access control security. Use GetAccessControlto get the ACL for the registry key, modify it, and then save it with SetAccessControl.
您可能需要修改访问控制安全性。使用GetAccessControl获取注册表项的 ACL,修改它,然后使用SetAccessControl保存它。
回答by Last Resort
Add an app.manifest in Visual studio
在 Visual Studio 中添加 app.manifest
add item then search for application manifest file
添加项目然后搜索应用程序清单文件
then Replace the start of it until with this
然后替换它的开始直到用这个
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
</requestedPrivileges>
</security>
</trustInfo>
回答by daryal
First login with Local Admin account. Then run regedit.exe and click on the registry key to be edited. From the Edit menu select premissions. Check the permissions of the related user.
首先使用本地管理员帐户登录。然后运行regedit.exe,点击要编辑的注册表项。从编辑菜单中选择权限。检查相关用户的权限。
I guess related accounts do not have permission to modify the registry key.
我猜相关帐户没有修改注册表项的权限。
回答by Arpit
in vb.net you can use this to get access of the key before doing any operation on it
在 vb.net 中,您可以在对其进行任何操作之前使用它来访问密钥
x.GetAccessControl(System.Security.AccessControl.AccessControlSections.All)
where x is any reg. key
其中 x 是任何 reg。钥匙
after seeing your code i tried this and it works
看到你的代码后,我尝试了这个并且它有效
Dim r As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\test", Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree)
r.DeleteSubKey("subkey", True)

