简体   繁体   中英

what is wrong with this code to read specific registry value in a subkey then do a if exist/if not statement

well with some help from this site and dream in code i fixed my error, but am now the code just plain doesn't seem to be working. Here is the code:

using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\"))
if (Key != null)
{
    string val = (string)Key.GetValue("COMODO Internet Security");
    if (val == null)
    {
        MessageBox.Show("value not found");
    }
    else
    {
        // use the value
    }
}
else
{
    MessageBox.Show("key not found");
}

and when i use it, it give the "value not found" message box like its supposed to if it doesn't exist, only problem is the value DOES exist... i checked manually through regedit. so whats wrong?

Probably you have the problem to query registry values of HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run key on 64-bit operation system from a 32-bit application. In the case you will see values existion only under HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run (see http://msdn.microsoft.com/en-us/library/aa384232.aspx ). If you use unmanaged API (or invoce) you can open Run key with RegOpenKeyEx and KEY_QUERY_VALUE | KEY_WOW64_64KEY KEY_QUERY_VALUE | KEY_WOW64_64KEY flags (see http://msdn.microsoft.com/en-us/library/aa384129.aspx ). You should test whether the application run under 64-bit operation system before using KEY_QUERY_VALUE | KEY_WOW64_64KEY KEY_QUERY_VALUE | KEY_WOW64_64KEY flag. In case of 32-bit operation system you should use KEY_QUERY_VALUE flag only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM