Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why I am not able to access a value in the dictionary in C#?

 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
Pls help me to do so. Thanks in Advance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading