这段代码有什么问题吗? [英] what's wrong here with this code?
问题描述
大家好
我是c#
注册表编程的新手我有代码,这没有任何错误,但它是不工作!
我想在Windows 8.1 Pro上禁用Hibernate N
这里有什么问题?< br $> b $ b
private void button1_Click( object sender,EventArgs e)
{
尝试
{
RegistryKey mykey = Registry.LocalMachine.OpenSubKey( SYSTEM \\CurrentControlSet \\Control \\Power, true );
mykey.SetValue( HibernateEnabled, 0 跨度>);
mykey.Close();
}
catch {}
}
非常感谢:)(div class = h2_lin>解决方案
您可能需要管理员权限才能更改注册表的SYSTEM区域,因此您的修改将被拒绝。
这是一个很好的例子,为什么吞咽异常是一个坏主意:你不知道有问题或任何细节,只是代码不起作用。
更改你的收益:
catch (例外情况)
{
Debug.WriteLine(ex.Message);
}它会告诉你问题是什么。
几乎可以肯定,这是一个授权问题。这可能是一种更好的方法: https://social.msdn.microsoft.com/Forums/vstudio/en-US/386e0268-8986-4e49-8023-f9ae2e2689ab/disabling-hibernation?forum=csharpgeneral [ ^ ]
hi everybody
I'm new to registry programming with c#
I have a code, this has not any error but it's not working!
I want to disable Hibernate on windows 8.1 Pro N
what's wrong here?
private void button1_Click(object sender, EventArgs e)
{
try
{
RegistryKey mykey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Power", true);
mykey.SetValue("HibernateEnabled", 0);
mykey.Close();
}
catch { }
}
thanks a lot :)
The chances are that you need admin permission to change the SYSTEM area of the registry and your modification is being rejected as a result.
This is a good example of why swallowing exceptions is a bad idea: you have no idea there is a problem, or any details about it, just that the code doesn't work.
Change your catch:
catch (Exception ex) { Debug.WriteLine(ex.Message); }
And it will tell you what the problem is.
Almost certainly, it's an authorization problem. This may be a better way to do it: https://social.msdn.microsoft.com/Forums/vstudio/en-US/386e0268-8986-4e49-8023-f9ae2e2689ab/disabling-hibernation?forum=csharpgeneral[^]
这篇关于这段代码有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!