Create registry key programmatically - c#

I'm working on a C# project and I'm very confused about the creation of a registry key.
I have a Wix Installer. And the and of the setup File, i'm calling a custom action to create sub key (I'v try with the wix feature but it didn't work).
My custom action is :
RegistryKey Nkey = Registry.LocalMachine;
RegistryKey valkey = Nkey.OpenSubKey(Manager.REGKEY, true); //=> REGKEY = "Software\\MyService"
if (valkey == null)
{
valkey = Nkey.CreateSubKey(GestionCertificats.REGKEY);
}
valkey.Close();
Registry.LocalMachine.CreateSubKey(#"SYSTEM\CurrentControlSet\services\eventlog\Application\MyService");
After install, I can see the second key but no the first one. I'm not sure to right understand the operation of the keys. I'm working on a windows 7 64 bit, I'm compiling with "Any CPU" but my application seems to be in x86.
I've try to debug the action. value valkey is not null, but I didn't see the key with regedit. By forcing the CreateSubKey I still have no key.
I don't know what to do, I need help.

just a guess, because everything looks okay.
try closing the key returned. The description of the dispose of a key it clears resources but doesn't say it flushes it.
RegistryKey key = Registry.LocalMachine.CreateSubKey(#"SYSTEM\CurrentControlSet\services\eventlog\Application\MyService");
key.Close();

Related

How Is This Returning NULL

My C# application license manager is returning NULL when checking for a Key's existence even though the key exists and my application is installed. I have tried running as an Administrator and add or removing backslashes in the Key path.
RegistryKey LitenUpKey = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\LitenUp\NIT", false);
if (LitenUpKey == null) {
// Registry Key NOT Found
return false;
}
NOTE: I am building as x64!
As #RbMm pointed out, the issues was in registry reflection between 32 bit and 64 bit. The following question showed me how to choose which view I saw. Here it is.

Change MachineGuid C#

i was wondering for a project of mine, is it possible to change the MachineGuid from the registry or any other way? I've seen it in multiple applications and I can't do it myself..This is my code
RegistryKey reg = Registry.LocalMachine;
reg.OpenSubKey("SOFTWARE\\Microsoft\\Cryptography");
reg.DeleteValue("MachineGuid");
reg.Close();
consolebox.AppendText("MachineGuid should be changed!\n");
But it doesn't work.. it doesn't delete the MachineGuid value, which would automatically regenerate in about a second....
The error says that it doesnt find the value.. that MachineGuid doesn't exist... but when i go to regedit it does?
If i don't run the application as an Administrator, it says the value got deleted, but if i do it says it doesn't exist....
You have a couple of issues, first you don't open the key to be writeable and you don't use the result of OpenSubKey. That method returns the key you actually opened.
RegistryKey reg = Registry.LocalMachine;
using(var key = reg.OpenSubKey("SOFTWARE\\Microsoft\\Cryptography", true)) // writeable
{
key.DeleteValue("MachineGuid");
}
The RegistryKey object implements IDisposable, better apply the using pattern in that case to close and dispose the key.

Delete all values within a registry key using C#

I'm having trouble finding a method to delete all values within a registry key without actually deleting the key itself. I would rather not have to delete the key because I'd rather not deal with re-adding the appropriate permissions back to the key.
I'm trying to have a small method run to simply clear out the values from:
HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Outlook\Resiliency\DisabledItems
We have an add-in that Outlook periodically sticks in here, and our techs have to dig into this key and remove the values, restart Outlook and all is well.
I guess we could just identify the exact name of the value, but that wouldn't be very fun :) and because the way that the values are named in this key, it is not apparent which one points to the correct add-in.
I agree with Ron Beyer. I think this might be what you are looking for? Edit the registry paths and HKLM/HKCU as needed.
string keyPath64Bit = "SOFTWARE\\Wow6432Node\\Krondorian";
RegistryKey localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey key64Bit = localMachine.OpenSubKey(keyPath64Bit, true);
if (key64Bit != null)
{
var namesArray = key64Bit.GetValueNames();
foreach (string valueName in namesArray)
{
key64Bit.DeleteValue(valueName);
}
}

Can't Read Registry Key

give the code below, lastuser string returns null, however, if I use regedit to look at this key it has data associated with it. Is LoggedOnSAMuser a restricted key?
public static string lastlogon()
{
string lastuser;
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI",false);
if (registryKey != null)
{
lastuser = (string) registryKey.GetValue("LastLoggedOnSAMUser");
}
else lastuser = "Unknown User";
return (lastuser);
}
2 possible issues:
You are trying to read the LoggedOnSAMUser key, quite a chance you
meant LastLoggedOnSAMUser.
You might be trying to read a 64-bit registry entry from a 32-bit application. If possible, change your platform target to x64 and retry. If not possible, you might have to use the registry API directly. Hopefully a nudge in the right directon: link
Almost certainly you have a 32 bit process on a 64 bit machine and so are subject to registry redirection. Your 32 bit process, by default, reads from the 32 bit view of the registry. But you want to read from the 64 bit view.
Solve the problem by requesting that you read from the 64 bit view of the registry, by way of the RegistryView enumeration.
This seems to work on Windows 7
RegistryKey thisKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey thisSubkey = thisKey.OpenSubKey(#"SOFTWARE\\fred", false);
_url = (string)thisSubkey.GetValue("_url", "*");
_port = (string)thisSubkey.GetValue("_port", 0);

Delete Subkey error (C#)

I have created the following registry key (copied through regedit):
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\test
I would like to now delete this registry key, and so... I have been using the following code and am running into a small error.
RegistryKey regKey;
string regPath_Key = #"Software\Microsoft\Windows\CurrentVersion\test";
regKey = Registry.CurrentUser.OpenSubKey(regPath_Key, true);
if(regKey != null) // Always returns null, even though the key does exist.
{
Registry.CurrentUser.DeleteSubKey(regPath_Key, true);
}
The issue I am having is that the line if(regKey != null) always returns null! I have gone back and checked that the key does in fact exist multiple times - but still the same result. I am going to assume my code has issues somewhere?
Could it be that you are on a 64 bit machine and your project is set to x86 architecture? in that case, verify that the key you state exists under HKCU\Software\Wow6432Node... as every path is redirected to this 32 bit process registry...
You should not include HKEY_CURRENT_USER in the string you pass to Registry.CurrentUser.OpenSubKey(). Instead use
string regPath_Key = #"Software\Microsoft\Windows\CurrentVersion\test";

Categories