I am using registry keys to access the path of an XML file from installed folder of my machine.
My machine has 32 bit OS. So I am using
using (RegistryKey pRegKey = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Wow6432Node\Folder\subfolder\key"))
{
if (pRegKey == null)
{
return;
}
else
// get path
But when I run it in 64 bit machine it is showing error, since I have not handled the exception case for 64 bit OS.
How can I handle it??
If your app is 32-bit and you want to install it both on 32-bit and 64-bit OS, use this:
string regPath = string.Format(#"SOFTWARE\{0}AppName\Folder\subfolder\key",
Environment.Is64BitProcess ? #"Wow6432Node\" : string.Empty());
using (RegistryKey pRegKey = Registry.LocalMachine.OpenSubKey(regPath))
{
.....
Related
I need to retrieve a string from the registry in 32/64 bit windows computers.
I have successfully retrieved info with registry.getvalue in a 64 bit environment, but it returns null in a 32 bit environment.
RegistryKey localKey;
localKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default);
string value = localKey.OpenSubKey(#"Software\MCS").GetValue("path").ToString();
if (value != null)
{
EventLog.WriteEntry("SMS_Service", "Path:" + value);
}
else
{
EventLog.WriteEntry("SMS_Service", "Registry path not found.");
}
Reg query Verification
event viewer
Everything checks out as far as I can tell. I feel like it has to be some kind of permission problem with the registry on the 32 bit machine.
This question already has answers here:
64 bit & 32 bit Registry issue in Windows (Programming in c#)
(2 answers)
Closed 8 years ago.
I'm having hard time with access the registry in my program (C#), here is the details :
My program run on 32 bit & 64 bit OS.
My program will need to get registry keys of software that some has 64 version and some not.
For example:
I'm running a 64 bit OS and i need to read 2 registry keys so i can know the install locations of the 2 different software, one has a 64 bit version so i don't need the "\Wow64node\" and one have only has 32 bit version so i will need the "\Wow64node\"
// 1.MSE: (64 bit version)
string installPath = (string)Registry.GetValue(#"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Antimalware", "InstallLocation", null);
if (installPath != null)
{
listViewAV.Items.Add("MSE");
}
// 2.Avast: (32 bit version)
installPath = (string)Registry.GetValue(#"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\AVAST Software\Avast", "ProgramFolder", null);
if (installPath != null)
{
listViewAV.Items.Add("Avast");
}
Notice that the Avast is in the Wow Node and MSE isnt, but if my program will be running on a 32 bit OS they both be without the Wow Node.
How can i write this code so it works on every scenario?
Scenario 1 : running on a 32 bit machine so all the softwares will be 32 bit as well(no "Wow64node").
Scenario 2 : running on a 64 bit machine so some of the softwares will be 32 bit and some and some 64 bit(need to handle with "Wow64node")
Please write a detailed answer(prefer with a code example).
Thank you
This is essentially the same question as you asked earlier today, and that I answered. I won't repeat my answer. What you are asking for is some code. I can give you that, but I fear that you still don't really understand the registry redirector. I think you are going to have to get on top of that before you can expect to make any progress.
To read a value from the 32 bit view of the registry, for instance Avast, use this code:
RegistryKey rk = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
RegistryView.Registry32);
rk = rk.OpenSubKey(#"SOFTWARE\AVAST Software\Avast");
string installPath = null;
if (rk != null)
{
installPath = rk.GetValue("ProgramFolder", null);
}
if (installPath != null)
{
.... // Avast registry setting found
}
For a program registered in the 64 bit view of the registry, for instance MSE, do it like this:
RegistryKey rk = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
RegistryView.Registry64);
rk = rk.OpenSubKey(#"SOFTWARE\Microsoft\Microsoft Antimalware");
string installPath = null;
if (rk != null)
{
installPath = rk.GetValue("InstallLocation", null);
}
if (installPath != null)
{
.... // Avast registry setting found
}
You don't need to worry about whether or not you are running on a 32 bit or 64 bit system. On a 64 bit system, both views exist, and you get the view that you request. On a 32 bit system, only the 32 bit view exists, and the value of the RegistryView enumeration that you pass is ignored. The documentation says it like this:
If you request a 64-bit view on a 32-bit operating system, the returned keys will be in the 32-bit view.
I'm trying to read a registry key from my application. It is a 32-bit process and is running on a 64-bit system(Win7 64-bit). This is my code:
string value64 = string.Empty;
RegistryKey localKeyRegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
localKey = localKey.OpenSubKey(#"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
if (localKey != null)
{
value64 = localKey.GetValue("RegisteredOrganization").ToString();
MessageBox.Show(value64, "value64");
}
On my system the value under this key(SOFTWARE\Microsoft\Windows NT\CurrentVersion) is empty, and the value under this(SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion) is "Microsoft". But the value64 in the message box is empty! Shouldn't it be "Microsoft"?
You are specifying RegistryView.Registry64 so it will be retrieving the value from SOFTWARE\Microsoft\Windows NT\CurrentVersion
If you specify RegistryView.Registry32 it will retrieve it from SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion instead (that is, on a 64-bit system; on a 32-bit system the WOW6432Node doesn't exist so it will just use the normal hive instead).
I think that if you specify RegistryView.Default it will select the hive according to the bitness of the calling process.
So I'm creating an installer for the Java Access Bridge for my application and it is required to find the Java installation directory. I was using this piece of code which worked..
public static string GetJavaInstallationPath()
{
try
{
string environmentPath = Environment.GetEnvironmentVariable("JAVA_HOME");
if (!string.IsNullOrEmpty(environmentPath))
{
return environmentPath;
}
string javaKey = "SOFTWARE\\JavaSoft\\Java Runtime Environment\\";
using (Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(javaKey))
{
string currentVersion = rk.GetValue("CurrentVersion").ToString();
using (Microsoft.Win32.RegistryKey key = rk.OpenSubKey(currentVersion))
{
return key.GetValue("JavaHome").ToString();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
return null;
}
}
until.. I ran a clean Windows 7 64 bit install on my Virtual Machine and installed Java from java.com. It installed the 32 bit version of Java by default but I really thought it wouldn't matter because 32 bit would also require or the JAVA_HOME variable or the Registry Key. Well, this wasn't the case! There was no Registry Key, no entry in the PATH variable and there was no JAVA_HOME variable either. So this code wouldn't work! My question is, how would I detect the java installation directory even when it's the 32 bit Java version that's installed. There is nothing I know of that I can use..
You're forgetting about the registry path being different for 32bit application. See this MS article: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724072%28v=vs.85%29.aspx
I want to set a value for 'NoModify' in below registry path.
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\XXXX"
I am using below code and it works only for X86 machines. Can you see any reason why this is not working for x64 machines?
// This value is correct
RegistryView registryView = releaseFlags.Contains("PLATFORM_X86") ? RegistryView.Registry64 : RegistryView.Registry32;
using (RegistryKey hkeyLocalMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView))
{
RegistryKey noModifyKey = hkeyLocalMachine.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{xxxx}", true); //SL: Key Name
if (noModifyKey != null)
{
noModifyKey.SetValue("NoModify", 0);
noModifyKey.Flush();
}
}
When you are on a 64 bit machine and your app is 32 bit - it should store these settings in the HKLM\Software\WOW6432Node instead of the HKLM\Software\ node.
You should open base key in this way.
It works for me.
var rk = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
if (Environment.Is64BitOperatingSystem) {
rk = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
var val = rk.OpenSubKey("SOFTWARE\\Pourab\\sanjande", true);
val.SetValue("test", "testvalue");
}
As far as you compiles your .NET program as x86 not AnyCPU, you will be using the "correct" registry keys meant for x86 in any circumstances because it will be running as x86.
If you compile it as x64 or AnyCPU, it could be quite tricky because it will probably run as x64 on an x64 machine and uses the "wrong" registry where HKLM\SOFTWARE for x86 programs is actually HKLM\SOFTWARE\WOW6432Node.
It's my mistake in the code.
RegistryView registryView = releaseFlags.Contains("PLATFORM_X86") ? RegistryView.Registry64 : RegistryView.Registry32;
Should be as follows:
RegistryView registryView = releaseFlags.Contains("PLATFORM_X86") ? RegistryView.Registry32 : RegistryView.Registry64;