Way to write on registry location - c#

work on C# window application.I want to write on registry.i know how to write on registry.I use bellow syntax to write on registry.
Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.CurrentUser.cre.CreateSubKey("asb");
key.SetValue("asb", "Isabella");
key.Close();
But problem is i fail to write on specified location .i want to write on bellow location
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
On this location want to add string value="abc" and ValueData="efd"
If have any query plz ask.thanks in advance.

For HKCU:
string keyName = #"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
RegistryKey rk = Registry.CurrentUser.OpenSubKey(keyName, true);
rk.SetValue("abc", "efd");
rk.Close();
For HKLM you need to do it with administrative privileges. That requires adding a manifest to your program to invoke the UAC prompt on Vista or Win7.

Writing to HKEY_LOCAL_MACHINE requires administrative privileges. And if you're running on Windows Vista or 7, it also requires process elevation, lest you run afoul of UAC (User Account Control).
The best thing is only to write to this registry key during installation (where you will have full administrative privileges). You should only read from it once your application is installed.
Save all regular settings under HKEY_CURRENT_USER. Use the Registry.CurrentUser field to do that. Or, better yet, abandon the registry altogether and save your application's settings in a config file. Visual Studio has built-in support for this, it's very simple to do from C#. The registry is no longer the recommended way of saving application state.

RegistryKey reg = Registry.LocalMachine.
OpenSubKey(#"Software\Microsoft\Windows\CurrentVersion\Run", true);
// set value of "abc" to "efd"
reg.SetValue("abc", "efd", RegistryValueKind.DWord);
// get value of "abc"; return 0 if value not found
string value = (string)reg.GetValue("abc", "0");

Related

How to save windows standard user settings to the registry

We would like to change a PC's user to a standard user, but a program I created runs this PC reading and writing to the registry. Easiest solution would probably be to just use an ini file, but I would like to know if it is possible to still write to the registry when logged in as a standard user.
For starters would this still work (creating, reading and writing to a current user registry key)? And if not, is there a workaround?
code for reading:
Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("zebra_labelprint");
int isBigLabel = int.Parse(registryKey.GetValue("biglabel", "0").ToString());
registryKey.Close();
writing:
Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("zebra_labelprint");
registryKey.SetValue("biglabel", ""+value);
registryKey.Close();

In Visual Studio: Trying to delete registry entry but "Requested registry access is not allowed"

I have searched for quite a while on a solution to this. At least I think I understand the problem, but I have yet to come to any solution.
What I need is either some sort of executable or a script that will delete some registry entries. The problem is that the registry entries in question only give read/write access to SYSTEM and no one else. The only way that I can delete them is by going into regedit, setting myself as the owner, and finally setting full control to everyone. Only then can the keys be deleted. I need this process to be in some sort of script though!
So in C#, I first make sure that the software has administrative rights. Then I try to execute the following.
RegistryKey reg_localmachine = RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, "");
RegistryKey key = reg_localmachine.OpenSubKey(#"SYSTEM\path to subkey", true);
On the second line, when I try to access the sub key with write access, I get the exception "Requested registry access is not allowed." In order for me to change owner or grant permissions, I need to execute SetAccessControl() on the RegistryKey. In order to set access control, I need write privileges for the key. So I am in this paradox.
Security is there for a reason.
You don't want any old program to be able to come in and start hacking around in the registry.
Each entry in the registry has it's own set of DACLS. There are only two solutions:-
Change the account under which your program is running to an account that has permission to delete the registry entry.
Change the DACLS on the registry entry to include the account your program is running under.

Writing to registry local machine error

I want set default icon of some extension by C#. But this gives me error -> Security Exception was unhandled
RegistryKey FileExt = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Classes", RegistryKeyPermissionCheck.ReadWriteSubTree);
How can I do that?
try to run your application as administrator.
Maybe the user you are using to log in on the machine does not have privileged to access or modify the registry. try run the code with administrator account and see what happens. also if there is no user logged in the same error maybe occurs for instance check this.
I presume this is on Windows Vista or 7.
You may need to have elevated privileges to change some values in the registry. Even a user with administrative privileges will get the UAC pop up to ask permission before a program can do this.
Have a look at a question about this.
Here is a codeproject article about gaining elevated privileges.
try this method instead of yours:
public RegistryKey OpenSubKey(
string name,
bool writable
)
Maybe a true is enough ;-)
Registry.LocalMachine.OpenSubKey("SOFTWARE\\Classes", true);

Cannot Write to the Registry under HKEY_LOCAL_MACHINE\Software

I'm writing an application that needs to create a special user account hidden from login screens and the Control Panel users applet. By writing a DWORD value of 0 with the user name to the registry key below, I'm able to accomplish this goal:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Winlogon\SpecialAccounts\UserList
The problem is that under Windows 7 with UAC on, no matter what I try, I cannot programmatically write a value to the key above.
It is my understanding that writing to certain keys this is not allowed on Windows 7 with UAC on, unless you are running with Administrative privileges. I've added an application manifest requestedExecutionLevel level="requireAdministrator" uiAccess="false", I accept the UAC prompt when my program is run, my account is a member of Administrators, yet I am still unable to write to the above registry key.
What more do I need to do? How is it possible, in any application configuration, to write keys and values under HKEY_LOCAL_MACHINE\SOFTWARE?
Further information ... When my program runs, no errors are thrown and it seems to write values. My guess is that Windows is virtualizing the location to which I am writing. I need to write to the actual location, not a virtual one, if I am to hide this special user account.
Probably the program runs as 32-bit program on the 64-bit operation system? In the case I recommend you to search the values which you created under Wow6432Node subkey of the HKEY_LOCAL_MACHINE\SOFTWARE.
You can read more about such kind of virtualization here. You can use KEY_WOW64_32KEY flag in some API to be able to work with full registry without virtualization.
Write Value to Registry
string user = Environment.UserDomainName + "\\" + Environment.UserName;
RegistrySecurity rs = new RegistrySecurity();
rs.AddAccessRule(new RegistryAccessRule(user,
RegistryRights.WriteKey | RegistryRights.ChangePermissions,
InheritanceFlags.None, PropagationFlags.None, AccessControlType.Deny));
RegistryKey rk = null;
try
{
rk = Registry.CurrentUser.CreateSubKey("SOFTWARE\\TEST",
RegistryKeyPermissionCheck.Default, rs);
rk.SetValue("NAME", "IROSH);
rk.SetValue("FROM", "SRI LANKA");
}
This could have something to do with the redirection they added in Vista. I would be curious if you tried to read that registry value from your code, if you would get back the value you were expecting. You may also want to fire up RegMon to see if you can see where the redirection may be forcing you.
RegistryKey rk = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);
rk.SetValue("Name", "Value");

How to read registry branch HKEY_LOCAL_MACHINE in Vista?

I have Application settings stored under HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany branch. Settings must be same for different users and that is the reason why settings are not under HKEY_CURRENT_USER. Registry values are only read during use of application.
Now, in Windows Vista and due to UAC you can't anymore use following code to read registry values:
RegistryKey myKey = Registry.LocalMachine.CreateSubKey
("SOFTWARE\\MyCompany\\MyAppName");
How can I read the values from LocalMachine branch in my code (C#)?
The problem is that you are trying to create a key not read it. You should be able to read values from HKLM just fine on Vista if you use the appropriate API.
RegistryKey myKey = Registry.LocalMachine.OpenSubKey(
#"Software\MyCompany\MyAppName",
false);
Notice the false parameter in the above. This has the effect of opening the key in a read only mode. This is the default setting for OpenSubKey but I prefer to be explicit (mainly because I can't ever remember the default).

Categories