如何在C#中更改受保护的注册表项的值? [英] How to change the value of a protected registry key in C #?

查看:183
本文介绍了如何在C#中更改受保护的注册表项的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我想更改一些系统注册表项值,但我不明白该怎么做。要测试更改,我使用双键NetworkThrottlingIndex并尝试将值从0xffffffff更改为0x0000000a。我的程序以完全权利运行。





首先尝试:

< pre lang =c#> RegistryKey regKey = Registry.LocalMachine.OpenSubKey( @ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \Multimedia \ SystemLrofile true );
regKey.SetValue( NetworkThrottlingIndex 10 ,RegistryValueKind.DWord);





第二次尝试:

  string  RegKeyPath = Path.Combine(Environment.GetEnvironmentVariable(  TEMP), @  TEST\user.reg ); 
if (File.Exists(RegKeyPath))
File.Delete(RegKeyPath);

StreamWriter createRegKeyText = File.CreateText(RegKeyPath);
createRegKeyText.WriteLine( Windows注册表编辑器版本5.00);
createRegKeyText.WriteLine( );

regKeyRootString = @ HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft \ Windows NT \ CurrentVersion \Multimedia\SystemProfile;
regKeyString = NetworkThrottlingIndex;
regValueString = = dword:0000000a;

createRegKeyText.WriteLine( [ + regKeyRootString + ]);
createRegKeyText.WriteLine(' \ u0022' + regKeyString + ' \ u0022' + regValueString);
createRegKeyText.WriteLine( );
createRegKeyText.Close();

处理addToReg = new Process();
addToReg.StartInfo.FileName = Path.Combine(Environment.GetEnvironmentVariable( WINDIR) , regedit.exe);
addToReg.StartInfo.Arguments = / s + RegKeyPath;
addToReg.Start();





我知道第二次尝试并不是最好的。但我不明白为什么一切都行不通。在第二次尝试时,我看到文件已正确创建。如果我打开文件manuel双击它工作,我可以添加该键,值已更改。但为什么它不适用于我的简单工具?!



我认为它是受保护的密钥,但必须有办法改变它。



我希望有人可以告诉我什么是错的...



Greets ...

解决方案

我看到......

Wow6432是32位注册表配置单元,是可见的32位应用程序。您可能正在以32位进程运行您的应用程序。此过程只能访问注册表中的32位值,即Wow6432节点下的值。然而,64位应用程序可以做到这两点。由于您使用的是64位操作系统,因此您的密钥会在Wow6432节点上创建,但您的应用程序会将其视为原始位置。这是'内部处理与32位分开的64位。但是,在32位操作系统上,密钥将按预期创建。



将您的应用程序编译为64位。


您的问题是两次尝试都提升到管理员。

在您的第一次尝试中,您应该能够通过嵌入具有此条目的应用程序清单来确保应用程序以提升的权限运行:

 <   requestedexecutionlevel     level   =  requireAdministrator     uiaccess   = < span class =code-keyword> false    /  >  



在这种情况下,您的应用程序只会在提升的权限下运行ons。



关于第二次尝试,我同意这不是首选方式。

但至于为什么它不起作用,你需要在高架模式下启动regedit。

添加此代码应该这样做:



 addToReg.StartInfo.Arguments =   / s + RegKeyPath; 
addToReg.StartInfo.Verb = runas; // 这将使流程开始提升
addToReg.Start() ;





但是我不推荐这种方法。


编辑:



发现问题......我感觉很蠢......:D



应用程序在以下路径中创建或更改了密钥...

 [HKEY_LOCAL_MACHINE\SOFTWARE \Wow6432Node\Microsoft \ Windows NT \ CurrentVersion \ Multimedia\SystemProfile] 





但我想在这里进行更改:

 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft \ Windows NT \ CurrentVersion \Multimedia \ SystemProfile] 





有人知道怎么做只更改x86键?


Hello,

I want to change some system registry key values, but I can''t understand how to do that. To test the changes I use the dword key "NetworkThrottlingIndex" and try to change the value from 0xffffffff to 0x0000000a. My program runs with full rights.


First try:

RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile", true);
regKey.SetValue("NetworkThrottlingIndex", 10, RegistryValueKind.DWord);



Second try:

string RegKeyPath = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), @"TEST\user.reg");
if (File.Exists(RegKeyPath))
	File.Delete(RegKeyPath);

StreamWriter createRegKeyText = File.CreateText(RegKeyPath);
createRegKeyText.WriteLine("Windows Registry Editor Version 5.00");
createRegKeyText.WriteLine("");

regKeyRootString = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile";
regKeyString = "NetworkThrottlingIndex";
regValueString = "=dword:0000000a";

createRegKeyText.WriteLine("[" + regKeyRootString + "]");
createRegKeyText.WriteLine('\u0022' + regKeyString + '\u0022' + regValueString);
createRegKeyText.WriteLine("");
createRegKeyText.Close();

Process addToReg = new Process();
addToReg.StartInfo.FileName = Path.Combine(Environment.GetEnvironmentVariable("WINDIR"), "regedit.exe");
addToReg.StartInfo.Arguments = "/s " + RegKeyPath;
addToReg.Start();



I know the second try isn''t the best. But I can''t understand why nothing will work. At the second try I see the file was correctly created. If I open the file manuel with double click it works, I can add that key and the value is changed. But why it will not work over my simple tool?!

I think it''s a protected key, but there must be a way to change it.

I hope someone can tell me whats wrong...

Greets...

解决方案

I see...
Wow6432 is the 32 bit registry hive that is visible 32 bit apps. Probably you are running your app as a 32 bit process. Such process can only access 32 bit values in the registry i.e, values under Wow6432 node. Whereas, 64 bit apps can do both. Since you are using a 64 bit OS, your key gets created on Wow6432 node but your app sees it as if it were on the original place. That''s windows internal dealing of separate 64 bit from 32 bit. However, on a 32 bit OS, the key will be created as expected.

Compile your app as 64 bit.


Your problem is the elevation to administrator in both tries.
In your first try you should be able to ensure that the app runs with elevated rights by embedding an App manifest that has this entry:

<requestedexecutionlevel level="requireAdministrator" uiaccess="false" />


In this case your application will only run with elevated permissions.

Regarding the second try, I agree this is not the preferred way.
But as for why it doesn''t work, you need to start the regedit in elevated mode as well.
Adding this code should do this:

addToReg.StartInfo.Arguments = "/s " + RegKeyPath;
addToReg.StartInfo.Verb = "runas"; // this will make the process start elevated
addToReg.Start();



However I would not recommend this approach.


EDIT:

Problem detected... I''m feeling so stupid... :D

The application created or changed the key at following path...

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile]



But I want the changes here:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile]



Someone know how to change only the x86 keys?


这篇关于如何在C#中更改受保护的注册表项的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆