为什么此尝试更新注册表值失败? [英] Why does this Attempt to Update a Registry Value Fail?
问题描述
我有此代码:
RegistryKey myKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Android Studio", true);
myKey.SetValue("StartMenuGroup", "Droidio", RegistryValueKind.String); // was "Android Studio" - change to "Droidio"
...我从此处.
运行它,尽管会导致以下NRE转储:
Running it, though results in the following NRE dump:
*System.NullReferenceException was unhandled
_HResult=-2147467261
_message=Object reference not set to an instance of an object.
HResult=-2147467261
IsTransient=false
Message=Object reference not set to an instance of an object.
Source=Sandbox
StackTrace:
at Sandbox.Form1.button57_Click(Object sender, EventArgs e) in c:\HoldingTank\Sandbox\Form1.cs:line 2524
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Sandbox.Program.Main() in c:\HoldingTank\Sandbox\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:*
我要更新的注册表设置(作为测试;然后将其重新设置)在这里:
The registry setting I'm trying to update (as a test; I'll then set it back) is here:
当注册表项清楚地出现在这里时,怎么会失败?
How could this be failing when the registry key is plainly there?
我尝试了一种替代方法,该方法从此处改编而来:
I tried an alternative approach, which I adapted from here:
RegistryKey reg32key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
RegistryView.Registry32);
RegistryKey reg_32bit_AppKey =
reg32key.OpenSubKey(@"HKEY_LOCAL_MACHINE\SOFTWARE\Android Studio");
if (reg_32bit_AppKey != null)
{
MessageBox.Show(String.Format("value was {0}",
reg_32bit_AppKey.GetValue("StartMenuGroup").ToString()));
reg_32bit_AppKey.SetValue("StartMenuGroup", "Droidio");
MessageBox.Show(String.Format("value is now {0}",
reg_32bit_AppKey.GetValue("StartMenuGroup").ToString()));
}
else
{
MessageBox.Show("Cannot open registry");
}
...但是,尽管它没有崩溃,但我看到"无法打开注册表"消息.
...but, while it doesn't crash, I see the "Cannot open registry" message.
以此(使位数增加一倍):
With this (doubling the bitness):
RegistryKey reg64key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
RegistryView.Registry64);
RegistryKey reg_64bit_AppKey =
reg64key.OpenSubKey(@"HKEY_LOCAL_MACHINE\SOFTWARE\Android Studio");
if (reg_64bit_AppKey != null)
{
MessageBox.Show(String.Format("value was {0}",
reg_64bit_AppKey.GetValue("StartMenuGroup").ToString()));
reg_64bit_AppKey.SetValue("StartMenuGroup", "Droidio");
MessageBox.Show(String.Format("value is now {0}",
reg_64bit_AppKey.GetValue("StartMenuGroup").ToString()));
}
else
{
MessageBox.Show("Cannot open registry");
}
...我仍然得到"无法打开注册表"结果.
...I still get the "Cannot open registry" result.
好的,将代码更改为此(从替代尝试中删除"HKEY_LOCAL_MACHINE"):
Okay, changing the code to this (removing the "HKEY_LOCAL_MACHINE" from the alternative attempt):
RegistryKey reg64key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
RegistryView.Registry64);
RegistryKey reg_64bit_AppKey = reg64key.OpenSubKey(@"SOFTWARE\Android
Studio");
if (reg_64bit_AppKey != null)
{
MessageBox.Show(String.Format("value was {0}",
reg_64bit_AppKey.GetValue("StartMenuGroup").ToString()));
reg_64bit_AppKey.SetValue("StartMenuGroup", "Droidio");
MessageBox.Show(String.Format("value is now {0}",
reg_64bit_AppKey.GetValue("StartMenuGroup").ToString()));
}
else
{
MessageBox.Show("Cannot open registry");
}
...更改异常.不过,首先,有一些进展,正如我所看到的,"值是Android Studio "
...changes the exception. First, though, there is some progress, as I see, "value was Android Studio"
...但随后它在下一行中断,即:
...but then it breaks on the next line, namely:
reg_64bit_AppKey.SetValue("StartMenuGroup", "Droidio");
...具有:
*System.UnauthorizedAccessException was unhandled
_HResult=-2147024891
_message=Cannot write to the registry key.
HResult=-2147024891
IsTransient=false
Message=Cannot write to the registry key.
Source=mscorlib
StackTrace:
at System.ThrowHelper.ThrowUnauthorizedAccessException(ExceptionResource resource)
at Microsoft.Win32.RegistryKey.EnsureWriteable()
at Microsoft.Win32.RegistryKey.SetValue(String name, Object value, RegistryValueKind valueKind)
at Microsoft.Win32.RegistryKey.SetValue(String name, Object value)
at Sandbox.Form1.button57_Click(Object sender, EventArgs e) in c:\HoldingTank\Sandbox\Form1.cs:line 2559
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentM
anager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Sandbox.Program.Main() in c:\HoldingTank\Sandbox\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object
state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state,
Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:*
那为什么我可以读而不是写?我在运行此代码时以管理员模式运行Visual Studio 2013 ...
So why can I read, but not write? I am running Visual Studio 2013 in Administrator mode as I run this code...
这是一个未经授权的访问问题.
It's an unauthorized access problem now.
这是我到达设置值的行时的reg_64bit_AppKey的值:
Here's the value of reg_64bit_AppKey as I reach the line to set the value:
通过它进行咳嗽会引起未经授权的访问"的抱怨.
F10ing through it coughs up the "Unauthorized access" whin[g]ing.
创建具有以下内容的文件:
Creating a file with this content:
REGEDIT4
[HKEY_LOCAL_MACHINE\SOFTWARE\Android Studio]
"StartMenuGroup"="Droidio"
...将其命名为" DroidioReg.reg ",然后从Windows资源管理器中手动选择合并"上下文菜单即可达到我的预期(还是希望如此):将StartMenuGroup的值更改为"Droidio".实际上,即使只是单击两下文件并在所有确认对话框中移动也是如此.
...naming it "DroidioReg.reg", and manually selecting the Merge context menu from Windows Explorer did what I expected (hoped, anyway): changed StartMenuGroup's value to "Droidio". Actually, even just 2-clicking the file and moving through all the confirmation dialogs works, too.
因此,这很麻烦,但是可以以编程方式在该文件上调用合并"吗-IOW,将该文件放在手持设备上,然后以代码形式执行它?用户将不得不浏览警告对话框,还是有办法以编程方式阻止那些对话框显示?
So, this is kludgy to the Nth degree, but is it possible to programatically call "Merge" on that file - IOW, place that file on the handheld device and then, in code, execute it? Will the user have to navigate through the cautionary dialogs, or is there a way to programmatically prevent those from displaying?
推荐答案
使用转义的字符串而不是字符串文字. OpenSubKey方法似乎无法正确读取您的转义路径.
Use an escaped string instead of a string literal. The OpenSubKey method doesn't appear to read your escaped path properly.
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Android Studio", true);
这篇关于为什么此尝试更新注册表值失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!