Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# key Registry.
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...
Posted 2-May-13 3:02am
Si13n7375
Edited 2-May-13 3:15am
v4
Comments
Amitosh Swain at 2-May-13 9:55am
   
Are you running your app as an admin (Run as admin in context-menu)
lewax00 at 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 at 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!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v2
Comments
Si13n7 at 3-May-13 11:22am
   
Thanks! It works now. :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

EDIT:
 
Problem detected... I'm feeling so stupid... Big Grin | :-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?
  Permalink  
v4

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

  Print Answers RSS
0 OriginalGriff 520
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 265
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 3 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100