Home > Software engineering >  Why I am not able to access a value in the dictionary in C#?
Why I am not able to access a value in the dictionary in C#?

Time:10-05

     string[] keys = {
       @"SYSTEM\CurrentControlSet\Services\XblGameSave",
       @"SYSTEM\CurrentControlSet\Services\XboxNetApiSvc",
       @"SYSTEM\CurrentControlSet\Services\XboxGipSvc",
       @"SYSTEM\CurrentControlSet\Services\XblAuthManager",
       @"SYSTEM\CurrentControlSet\Services\wisvc",
       @"SYSTEM\CurrentControlSet\Services\Spooler",
       @"SYSTEM\CurrentControlSet\Services\PrintNotify",
       @"SYSTEM\CurrentControlSet\Services\MapsBroker",
       @"SYSTEM\CurrentControlSeSet\Services\bthserv",
       @"SYSTEM\CurrentControlSet\Services\DiagTrack",
       @"SYSTEM\CurrentControlSet\Services\dmwappushservice",
       @"SYSTEM\CurrentControlSet\Services\diagsvc",
       @"SYSTEM\CurrentControlSet\Services\diagnosticshub.standardcollector.service",
       @"SYSTEM\CurrentControlSet\Services\WdiServiceHost",
       @"SYSTEM\CurrentControlSet\Services\WdiSystemHost",
       @"SYSTEM\CurrentControlSet\Services\AJRouter",
       @"SYSTEM\CurrentControlSet\Services\ALG",
       @"SYSTEM\CurrentControlSet\Services\CertPropSvc",
       @"SYSTEM\CurrentControlSet\Services\Fax",
       @"SYSTEM\CurrentControlSet\Services\CscService",
       @"SYSTEM\CurrentControlSet\Services\RetailDemo",
       @"SYSTEM\CurrentControlSet\Services\seclogon",
       @"SYSTEM\CurrentControlSet\Services\SCardSvr",
       @"SYSTEM\CurrentControlSet\Services\ScDeviceEnum",
       @"SYSTEM\CurrentControlSet\Services\SCPolicySvc",
       @"SYSTEM\CurrentControlSet\Services\SysMain",
       @"SYSTEM\CurrentControlSet\Services\WerSvc",
       @"SYSTEM\CurrentControlSet\Services\DoSvc",
       @"SYSTEM\CurrentControlSet\Services\lfsvc",
       @"SYSTEM\CurrentControlSet\Services\SEMgrSvc",
       @"SYSTEM\CurrentControlSet\Services\PhoneSvc",
       @"SYSTEM\CurrentControlSet\Services\RemoteRegistry",
       @"SYSTEM\CurrentControlSet\Services\WdiServiceHost",
       @"SYSTEM\CurrentControlSet\Services\WdiSystemHost",
       @"SYSTEM\CurrentControlSet\Services\feafes",
       @"SYSTEM\CurrentControlSet\Services\ALG",
       @"SYSTEM\CurrentControlSet\Services\CertPropSvc",
       @"SYSTEM\CurrentControlSet\Services\Fax",
       @"SYSTEM\CurrentControlSet\Services\CscService",
       @"SYSTEM\CurrentControlSet\Services\WalletService",
       @"SYSTEM\CurrentControlSet\Services\WSearch",
       @"SYSTEM\CurrentControlSet\Services\W32Time",
       @"SYSTEM\CurrentControlSet\Services\tzautoupdate",
       @"SYSTEM\CurrentControlSet\Services\CDPSvc",
       @"SYSTEM\CurrentControlSet\Services\DusmSvc",
       @"SYSTEM\CurrentControlSet\Services\fhsvc",
       @"SYSTEM\CurrentControlSet\Services\PcaSvc",
     };

     Dictionary<string, uint> dict = new Dictionary<string, uint>();

     foreach (string key in keys)
     {
          RegistryKey rkey = Registry.LocalMachine.OpenSubKey(key, true);

          try
          {                    
                dict.Add(key, Convert.ToUInt32(rkey.GetValue("Start", RegistryValueKind.DWord))); }
          catch (Exception)
          {

          }
          finally
          {
                rkey?.Dispose();
          }

     }

Here I want to access the values of keys in the dictionary. I want to store them like value1 = the value of the first key, value2 = the value of the second key, and so on.

uint value = dict["@\"SYSTEM\\\\CurrentControlSet\\\\Services\\\\XblGameSave\""];

I tried this code but it shows that 'The given key was not present in the dictionary.' The exception it gives is:

System.Collections.Generic.KeyNotFoundException

Please help me to do so. Thanks in Advance.

CodePudding user response:

The @ before a string means the string is interpreted literally - that means the slashes \ will not mean anything like \r (carriage return) or \t (tab) it's just a literal slash : \r or \t (in this case).

Besides that you don't need to add extra "" to the string, so if you'd seek a value like so:

uint value = dict[@"SYSTEM\CurrentControlSet\Services\PcaSvc"];

You're good to go.

You could also write this as

uint value = dict["SYSTEM\\CurrentControlSet\\Services\\PcaSvc"];

But you don't need to add \\ (4!) slashes for each slash

To explain a bit further what you're attempting is to seek this:

   @"SYSTEM\\CurrentControlSet\\Services\\XblGameSave"

Which will (for obvious reasons) not work

  • Related