CreateSubKey is not creating entry in current user - c#

I am trying to create a sub key under Current user, Application runs successfully, but unable to create sub key under current user. Following is my application logic. Can any one tell me what mistake I am doing here?
RegistryKey myAppSettings = Registry.CurrentUser.CreateSubKey(#"SOFTWARE\MyAppSettings");
Console.WriteLine(myAppSettings);
//storing the values
myAppSettings.SetValue("appName", "C# Tutorials");
myAppSettings.SetValue("Demo", "Registry keys storage");
myAppSettings.Close();

CreateSubkey does not work with path values. You have to open the RegistryKey "software" and create the subkey for it.

Related

OpenSubKey() Registry key's “Absolute Path”?

Using Microsoft.Win32.RegistryKey C# functions which require a registry path, like OpenSubKey(), using a path like
#"SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"
generates an error stating “Absolute path information is required.”
What is the syntax to create the absolute path required?
The registry has a couple of root keys and all subkeys are relative to one of these.
In order to use the OpenSubKey method, you must have an instance of the RegistryKey method. To get an instance of RegistryKey, use one of the static members of the Registry class.
If for example you want the key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet as seen in Regedit you would have to start with Registry.LocalMachine.
RegistryKey rk = Registry.LocalMachine.OpenSubKey(#"SYSTEM\CurrentControlSet");
... = rk.GetValue(...);
If you already have a key, yourkey.Name is the path of the key.

Accessing all the keys under classesroot from registry with c#

I have a program which i try is to read all the keys under classesroot but when i do it with GetSubKeyNames() many keys are skipped i only get the key which contains another sub key on it.
RegistryKey key = Registry.ClassesRoot;
foreach (string tempKeyName in key.GetSubKeyNames())
{
MessageBox.Show(tempKeyName);
}
enter image description here
This code shows all the registry keys under HKEY_CLASSES_ROOT. If it doesn't work for you, edit your question telling us Windows version and maybe if you are under a corporate network.
RegistryKey rk = Registry.ClassesRoot;
// Print out the keys.
PrintKeys(rk);
PrintKeys:
static void PrintKeys(RegistryKey rkey)
{
// Retrieve all the subkeys for the specified key.
String[] names = rkey.GetSubKeyNames();
Console.WriteLine("Subkeys of " + rkey.Name);
Console.WriteLine("-----------------------------------------------");
// Print the contents of the array to the console.
foreach (String s in names)
{
Console.WriteLine(s);
}
}
Source:MSDN
JANUARY 2022 - This issue is due to breaking changes introduced in the framework's registry searching library that apparently are not well known
For those searching and still coming up with incorrect registry key results this is due to the 32/64 redirecting in place by Microsoft. This means the old route of iterating for a key in the ClassesRoot hive using Registry.ClassesRoot could unknowingly search the Wow6432 key instead.
The new route is to look it up using the base key while explicitly defining where it should search. Here's the current way to correctly search for a key if you are having this trouble:
RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64)
Note the use of the RegistryKey static member instead of Registry
You can explicitly tell it to look in Registry32, Registry64, or Default - the later of which is what the old process of Registry.ClassesRoot appears to follow.

Cannot Delete Registry Entry even with Admin Rights

I have the following registry entry SOFTWARE\\mysoftware\\" in the localmachine
There are several subfolders and values within it.I just want to remove SOFTWARE\\myspftware\\
So i use
Registry.LocalMachine.DeleteSubKeyTree("SOFTWARE\\mysoftware\\");
in a program with admin rights. I keep getting the error
Cannot delete a subkey tree because the subkey does not exist.
even though the key exists.
Try
RegistryKey mykey = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
mykey.DeleteSubKeyTree("mysoftware");
mykey.Close();
I think the truein Registry.LocalMachine.OpenSubKey("SOFTWARE", true) is the magic part.

Finding Registry Keys in C#

I am working on a project that will allow me to delete the registry key from a Windows 7 PC. Specifically I am trying to make a program that will allow me to delete a profile from the machine via the ProfileList key. My problem is no matter what I try I can't seem to read the key correctly which I want to do before I start randomly deleting stuff. My code is
RegistryKey OurKey = Registry.LocalMachine;
OurKey = OurKey.OpenSubKey(#"SOFTWARE\Microsoft\WindowsNT\CurrentVersion\ProfileList", true);
foreach (string Keyname in OurKey.GetSubKeyNames())
{
MessageBox.Show(Keyname);
}
This code runs but doesn't return anything (No MessageBox). Any ideas why not?
EDIT:
I got the top level keys to load thanks to you all but it does only show the folder/key names (Ex: S-1-5-21-3794573037-2687555854-1483818651-11661) what I need is to read the keys under that folder to see what the ProfilePath is. Would there be a better way to go about that?
As pointed out by Lloyd, your path should use "Windows NT". In case of doubt, always use regedit to go inspect the registry manually.
Edit: To go with your edit, you can simply GetValue on the keys you find, the following code should do what you're looking for:
RegistryKey OurKey = Registry.LocalMachine;
OurKey = OurKey.OpenSubKey(#"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", true);
foreach (string Keyname in OurKey.GetSubKeyNames())
{
RegistryKey key = OurKey.OpenSubKey(Keyname);
MessageBox.Show(key.GetValue("KEY_NAME").ToString()); // Replace KEY_NAME with what you're looking for
}
Windows NT
Please do not miss space

Writing to registry in a C# application

I'm trying to write to the registry using my C# app.
I'm using the answer given here: Writing values to the registry with C#
However for some reason the key isn't added to the registry.
I'm using the following code:
string Timestamp = DateTime.Now.ToString("dd-MM-yyyy");
string key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\"+Application.ProductName+"\\"+Application.ProductVersion;
string valueName = "Trial Period";
Microsoft.Win32.Registry.SetValue(key, valueName, Timestamp, Microsoft.Win32.RegistryValueKind.String);
The Application.name and Application.version 'folders' don't exists yet.
Do I have to create them first?
Also, I'm testing it on a 64b Win version so I think if I want to check the registry for the key added I have to specifically check the 32bit registry in: C:\Windows\SysWOW64\regedit.exe don't I?
First of all if you want to edit key under LocalMachine you must run your application under admin rights (better use CurrentUser it's safer or create the key in installer). You have to open key in edit mode too (OpenSubKey method) to add new subkeys. I've checked the code and it works. Here is the code.
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software",true);
key.CreateSubKey("AppName");
key = key.OpenSubKey("AppName", true);
key.CreateSubKey("AppVersion");
key = key.OpenSubKey("AppVersion", true);
key.SetValue("yourkey", "yourvalue");
You can use the following code to create and open the required registry keys.
RegistryKey SoftwareKey = Registry.LocalMachine.OpenSubKey("Software",true);
RegistryKey AppNameKey = SoftwareKey.CreateSubKey("AppName");
RegistryKey AppVersionKey = AppNameKey.CreateSubKey("AppVersion");
AppVersionKey.SetValue("yourkey", "yourvalue");
You can basically use CreateSubKey for all your application settings, as it will open the key for write access, if it already exists, and create it otherwise. There is no need to create first, and then open. OpenSubKey comes in handy when you are absolutely certain the key already exists, like in this case, with "HKEY_LOCAL_MACHINE\SOFTWARE\"
Also check if your registry calls are getting virtualised. See here for more information.
It can happen if your application is not UAC aware and occurs for compatibility reasons.
Real path
HKEY_LOCAL_MACHINE\Software\FooKey
Virtual path
HKEY_USERS\<User SID>_Classes\VirtualStore\Machine\Software\FooKey
Try to open HKLM\Software first. Then create key for your program, and then create key for version. Howewer, your key could be placed at HKLM\software\WOW6432Node. Check this.
The problem is you don't have enough privileges. Here is a way that works for my:
RegistryKey myKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
myKey = myKey.OpenSubKey(subkey, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);
if (myKey != null)
{
myKey.SetValue("DefaultPrinterId", ldiPrinters[e.RowIndex].id, RegistryValueKind.String);
myKey.Close();
}
With RegistryKey.OpenBaseKey you open the correct registry, because when you don't have permissions the registry that you write, it does in another location.
By default, your changes will be written to HKLM\SOFTWARE\WOW6432Node\... because of registry redirection. This can be quite confusing.
In order to write to HKLM\SOFTWARE\..., you need to use RegistryKey.OpenBaseKey to open the 64-bit registry:
var path = #"SOFTWARE\...";
var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
var key = baseKey.CreateSubKey(path, RegistryKeyPermissionCheck.ReadWriteSubTree);
key.SetValue(name, value, RegistryValueKind.String);
Also, you need to have permission to write to the specified registry key.
You can get permission either by assigning permissions to specific users or service accounts or by running your app in elevated mode.

Categories

Resources