I am trying to read the recent
this is the code i have right now:
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Visual Studio\12.0\ProjectMRUList");
string data2 = (string)registryKey.GetValue("File1".ToUpper());
recentProjects.Items.Add(data2);
i keep getting a null error.
System.NullReferenceException: Object reference not set to an instance
of an object.
The error is on
string data2 = (string)registryKey.GetValue("File1");
The subkey is actually "VisualStudio", not "Visual Studio". Try the below:
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\VisualStudio\12.0\ProjectMRUList");
string data2 = (string)registryKey.GetValue("File1".ToUpper());
Or better still, you can have control over whether the environment is 32 or 64 bit, for example...
using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64))
{
using (var key = hklm.OpenSubKey(#"Software\Microsoft\VisualStudio\12.0\ProjectMRUList"))
{
string data2 = (string)key.GetValue("File1".ToUpper());
}
}
Related
I am trying to save the path of an application to a variable by using the registry.
What i want to achieve is:
1) Check if this application has an entry in the registry? (if it was installed or not)
2) If yes, I want to save the path to a variable which I can use later to use a program which is located in this path
So far i got
public void Run1()
{
Console.WriteLine("Reading OCR path from registry");
string keyName = #"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Tomcat\Common";
string valueName = "OCR_path";
string OCRPath = Microsoft.Win32.Registry.GetValue(keyName, valueName, null) as string;
if (string.IsNullOrWhiteSpace(OCRPath))
{
string text = "3";
System.IO.File.WriteAllText(#"C:\Users\Public\TestFolder\OCR-Toolkit-Check.txt", text);
}
}
Console.WriteLine("Reading OCR path from Registry:");
string keyName = #"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Tomcat\Common";
string valueName = "OCR_path";
if (Microsoft.Win32.Registry.GetValue(keyName, valueName, null) != null)
{
object variable = Microsoft.Win32.Registry.GetValue(keyName, valueName, null);
}
You might want to do the null-check, after reading it to a variable. Besides, you can cast the object variable to string afterwards.
I used a different approach and it works now. The path is saved into the variable OCRPath
public void Run1()
{
Console.WriteLine("Reading OCR path from registry");
string keyName = #"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Tomcat\Common";
string valueName = "OCR_path";
string OCRPath = Microsoft.Win32.Registry.GetValue(keyName, valueName, null) as string;
if (string.IsNullOrWhiteSpace(OCRPath))
{
string text = "3";
System.IO.File.WriteAllText(#"C:\Users\Public\TestFolder\OCR-Toolkit-Check.txt", text);
}
}
Holy moly, do not hardcode "Wow6432Node". You can get away with that on a 64 bit system opening the registry in 64-bit mode, but if you open the registry in 32-bit mode it will create a horrid thing you don't want to see. Also if you have a 32-bit OS, there is not supposed to be a "Wow6432Node" folder so you will end up creating stuff in places you shouldn't.
If you don't require opening the registry using only privileges and can rely on the permissions of the user to create/open/read keys then Microsoft already has Microsoft.Win32.Registry to help you.
string sPath = null;
RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
RegistryKey appKey = hklm.OpenSubKey(#"SOFTWARE\Tomcat\Common");
if(appKey != null)
{
object oPath = appKey.GetValue("OCR_path", null);
if(oPath != null && oPath is string)
{
sPath = oPath.ToString();
}
}
I am trying got do a simple read of a registry key but I cannot make it work even after reading many posts. What am I missing? I am running VS2015 as Administrator.
Exporting the key it is as follows
[HKEY_LOCAL_MACHINE\SOFTWARE\Test Key\dev]
"Enable"="TRUE"
I try to read it as follows
string myVal = (string)Registry.LocalMachine.GetValue(#"SOFTWARE\Test Key\dev\Enable");
MessageBox.Show(myVal);
I also tried (and variations)
RegistryKey key = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Test Key\dev");
string myVal = (string)key.GetValue("Enable");
MessageBox.Show(myVal);
I always get back NULL, why?
Are you on a 64 bits environment?
If so, try to set up the RegistryView parameter to make sure you access the 64 bit version of the registry:
using (var root = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
using (var key = root.OpenSubKey(#"SOFTWARE\Test Key\dev", false))
{
var myVal= key.GetValue("Enable");
MessageBox.Show(myVal.ToString());
}
}
If still doesn't work, try with RegistryView.Registry32 instead.
EDIT
You can actually set RegistryView dynamically using Environment.Is64BitOperatingSystem:
using (var root = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
Environment.Is64BitOperatingSystem
? RegistryView.Registry64
: RegistryView.Registry32))
{
using (var key = root.OpenSubKey(#"SOFTWARE\Test Key\dev", false))
{
var myVal= key.GetValue("Enable");
MessageBox.Show(myVal.ToString());
}
}
This will only work if the registry entry exists for the current platform (It could happen that it was just saved in the 32 bit version of the registry).
I'm trying to check (and delete) registry key. Code:
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
if (registryKey != null) // <- !!!!!!!!! problem is here, i think
{
registryKey.SetValue("MyApp", codeBase);
}
else
{
registryKey.DeleteValue("MyApp");
}
After creating value, application doesnt see new value and doesnt delete it.
What's wrong with this code? Thanks.
Your operations for adding and deleting from the registry look good just that there is an issue with your if-else condition. If you want to check and then delete a registry entry, you should do it like this:
string KeyName = #"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
string valueName = "MyApp";
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
if (Registry.GetValue(KeyName, valueName, null) != null)
{
RegistryKey registryKey = Registry.CurrentUser
.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
registryKey.DeleteValue(valueName);
}
Registry.GetValue(KeyName, valueName, null) is a better way to do a null check instead of doing registryKey != null because registryKey only gets the ..CurrentVersion\\Run subkey. Instead you should drill down the actual key for your app (eg. MyApp).
Hope this helps!
My below code fails no matter if I run it as Administrator or not:
var suff = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\CCM\\LocationServices", true);
var value = suff.GetValue("DnsSuffix").ToString();
I get this error message which I can't decode:
An unhandled exception of type 'System.NullReferenceException' occurred in MyApp.exe Additional information: Object reference not set to an instance of an object.
I know for a fact that the value exists and it contains data as well.
*Edit: So like I said it shouldn't be null as the data exists. And if it is null then I will need to know why is it null. Therefore, a question regarding what is System.NullReferenceException won't help me at all.
As raj's answer pointed out in this SO question, which is similar to yours, the problem could be that you're opening the registry on a 64bits OS.
Try this approach instead (.NET 4.0 or later) :
public class HKLMRegistryHelper
{
public static RegistryKey GetRegistryKey()
{
return GetRegistryKey(null);
}
public static RegistryKey GetRegistryKey(string keyPath)
{
RegistryKey localMachineRegistry = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32);
return string.IsNullOrEmpty(keyPath) ? localMachineRegistry : localMachineRegistry.OpenSubKey(keyPath);
}
public static object GetRegistryValue(string keyPath, string keyName)
{
RegistryKey registry = GetRegistryKey(keyPath);
return registry.GetValue(keyName);
}
}
... and replace your code with :
string keyPath = #"SOFTWARE\Microsoft\CCM\LocationServices";
string keyName = "DnsSuffix";
var value = HKLMRegistryHelper.GetRegistryValue(keyPath, keyName);
Reading the registry with "Registry.LocalMachine" can be unreliable as it defaults to the current application platform target (x86/x64) and when it's 64bit Registry.LocalMachine can view the key but can't access the data within.
Try specifying the view with RegistryKey.
var stuff = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
.OpenSubKey("Software\\Microsoft\\CCM\\LocationServices", true);
var value = stuff.GetValue("DnsSuffix").ToString();
I'm developing x86 app to translate self-created "programming language" to assembler. At some point I need to get path to MASM32. I have read a couple of related topics, but they didn't help me, maybe because I'm new to C#.
MASM32 is situated here:HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MASM32.
When I run my program, I always get "Masm32 is not found" message.
What should I do? Thanks in advance!
WindowsIdentity ident = WindowsIdentity.GetCurrent();
WindowsPrincipal myPrincipal = new WindowsPrincipal(ident);
if (!myPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
{
string user = Environment.UserDomainName + "\\" + Environment.UserName;
RegistrySecurity rs = new RegistrySecurity();
rs.AddAccessRule(new RegistryAccessRule(user,
RegistryRights.ReadKey | RegistryRights.Delete | RegistryRights.CreateSubKey | RegistryRights.WriteKey | RegistryRights.ChangePermissions | RegistryRights.SetValue,
InheritanceFlags.None,
PropagationFlags.None,
AccessControlType.Deny));
}
try
{
RegistryKey baseKey = RegistryKey.OpenBaseKey(
RegistryHive.LocalMachine,
RegistryView.Registry64);
RegistryKey key = baseKey.OpenSubKey(#"SOFTWARE\Wow6432Node\");
PathMasm32 = baseKey.GetValue("MASM32").ToString();
}
catch {
erTxt = "Masm32 is not found";
}
It is probably not OpenSubKey() that is causing the null reference exception, but the baseKey.GetValue().ToString() call. The baseKey.GetValue() returns null (because in that case you are trying to get a value right under the HKEY_LOCAL_MACHINE root node) and you invoke ToString() on a null reference. Instead of baseKey.GetValue(), you should try key.GetValue(), assuming MASM32 is really a value under HKLM\SOFTWARE\Wow6432Node which is highly unlikely.
key.GetValue("MASM32").ToString();
Security Note: If you are looking for the installation path of MASM32, even though I do not have any expertise on that, they clearly state that The MASM32 SDK is registry safe and writes nothing to the registry.
Thus, MASM32 is probably a KEY not a VALUE, therefore please execute this method and print the output of it, and you will see the key/value pairs registered under the MASM32 key assuming it exists at the registry path HKLM\SOFTWARE\Wow6432Node\MASM32
public static string GetMASM32LocationFromRegistry()
{
RegistryKey localMachineRegistryKey;
RegistryKey masm32RegistryKey;
RegistryView currentRegistryView = RegistryView.Registry64;
localMachineRegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, currentRegistryView);
masm32RegistryKey = localMachineRegistryKey.OpenSubKey(#"SOFTWARE\Wow6432Node\MASM32");
if (masm32RegistryKey == null)
{
return #"ERROR: The registry key HKLM\SOFTWARE\Wow6432Node\MASM32 could not be found";
}
StringBuilder masm32RegistryKeyValuesBuilder = new StringBuilder("Key/Value pairs for registry key HKLM\\SOFTWARE\\Wow6432Node\\MASM32:\r\n");
foreach (string masm32RegistryKeyValueName in masm32RegistryKey.GetValueNames())
{
masm32RegistryKeyValuesBuilder.AppendFormat
(
"Key: [{0}], Value: [{1}], Value Type: [{2}]\r\n",
masm32RegistryKeyValueName,
masm32RegistryKey.GetValue(masm32RegistryKeyValueName),
masm32RegistryKey.GetValueKind(masm32RegistryKeyValueName)
);
}
return masm32RegistryKeyValuesBuilder.ToString();
}