Click here to Skip to main content
15,920,603 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
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:
C#
RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile", true);
regKey.SetValue("NetworkThrottlingIndex", 10, RegistryValueKind.DWord);


Second try:
C#
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...
Posted
Updated 2-May-13 2:15am
v4
Comments
AlphaDeltaTheta 2-May-13 9:55am    
Are you running your app as an admin (Run as admin in context-menu)
lewax00 3-May-13 1:52am    
Are you sure YOU have the rights to do that? You can't very well give a program the proper permissions if you don't have them yourself (e.g. on a work computer, if it's you're own personal computer it's probably not an issue).
Bernhard Hiller 3-May-13 2:33am    
requestedexecutionlevel level="asInvoker" uiaccess="false" - That's it: your application does not run with administrative priviledges. Being a member of the admin group does not mean that all applications run in elevated mode!

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.
 
Share this answer
 
v2
Comments
s0mbr@ 3-May-13 11:22am    
Thanks! It works now. :)
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:
XML
<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:

C#
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.
 
Share this answer
 
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?
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900