asp.net - How to read Windows Server 2008 in c# -


i trying o read registry value following code.

    label1.text = registry.getvalue("hkey_local_machine\\software\\myweb\\connectionmanager", "id", null).tostring(); 

it work fine when trying in windows xp never works in windows server 2008. please

you running issue of wow redirector.

you can use more specialized classes in microsoft.win32 getting either 64 or 32 bits part of hive.

 var root = registrykey.openbasekey(registryhive.localmachine                                    ,registryview.registry32); // or registry64  var flk = root.opensubkey("software");  var slk = flk.opensubkey("myweb");  var tlk = slk.opensubkey("connectionmanager");  var val = tlk.getvalue("id");   label1.text = val.tostring(); 

or more general purpose method registry value whatever takes:

object getvalue64or32(string path, string valuekey) {      var parts = path.split('\\');      registryhive hive = registryhive.localmachine;      switch(parts[0])      {         case "hkey_local_machine":             hive = registryhive.localmachine;         break;         default:            throw new notimplementedexception();      }      foreach(var view in enum.getvalues(typeof(registryview)))      {         var key = registrykey.openbasekey(hive, (registryview) view);         for(var partindex=1; partindex<parts.length;partindex++)         {            key = key.opensubkey(parts[partindex]);            if (key == null) break;         }         if (key!=null) return key.getvalue(valuekey);      }      return null; } 

usage:

var value = getvalue64or32(                "hkey_local_machine\\software\\myweb\\connectionmanager"              , "id"); label1.text = value!=null?value.tostring():"no value found"; 

if use registry file code works 32 bits hive:

windows registry editor version 5.00  [hkey_local_machine\software\wow6432node\myweb\]  [hkey_local_machine\software\wow6432node\myweb\connectionmanager\] "id"="id 1" 

and works 64 bits hive:

windows registry editor version 5.00  [hkey_local_machine\software\myweb\]  [hkey_local_machine\software\myweb\connectionmanager\] "id"="id 1" 

you can use reg commandprompt verify if registrypath exists:

reg query hklm\software\myweb\connectionmanager /s 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -