Wednesday, August 15, 2018

Interacting with registry keys from C# should be a little different depending on whether you are in an x32 or x64 environment.

The approach to fishing something out of the registry needs to vary conditionally.

public string Fish(string lure)
{
   string fishingGrounds = @"SOFTWARE\Something\Er\Other\";
   if (!_myHashtable.ContainsKey(lure))
   {
      if (Environment.Is64BitOperatingSystem)
      {
         RegistryKey fish = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
               RegistryView.Registry64).OpenSubKey(fishingGrounds);
         _myHashtable.Add(lure, fish.GetValue(lure));
         fish.Close();
      }
      else
      {
         RegistryKey fish = Registry.LocalMachine.OpenSubKey(fishingGrounds);
         _myHashtable.Add(lure, fish.GetValue(lure));
         fish.Close();
      }
   }
   return _myHashtable[lure].ToString();
}

 
 

Environment is in the System namespace (System.Environment) and the registry flavored stuff is in the Microsoft.Win32 namespace.

No comments:

Post a Comment