简体   繁体   中英

ConfigurationManager.AppSettings[“KeyName”] not returning any value

In my app.config I have added a key with value

<add key="KeyName" value="someValue" />

But in my windows form when i try to access it by using configurationmanager it is not returning anything

ConfigurationManager.AppSettings["KeyName"]

this has null value Also i dont have any web.config or connection string added in the app.config, but my ConfigurationManager.connectionString returns some value !!

What might be the issue? what should i change?

PS I have added reference to system.configuration and added it in namespace too its an excel add-in and has only one class library which has app.config

You need to put all configurations you use is in the app.config file that belongs to the host application (the one that produce the executable file). If you put configuration in dll configuration file they will not be automatically picked up in the run time, and you will have to dynamically load those configuration using code similar to the one in this post. Not sure if this is your issue. Reading dll.config (not app.config!) from a plugin module As for where the connection is coming from, it is coming from the machine configuration file %windir%\\Microsoft.NET\\Framework64[version]\\config\\machine.config

If you have SQL Server including express installed a connection string will be added to this file

Have you checked if your application is returning other AppSetting Values ?

static void ReadAllSettings() { try { var appSettings = ConfigurationManager.AppSettings;

            if (appSettings.Count == 0)
            {
                Console.WriteLine("AppSettings is empty.");
            }
            else
            {
                foreach (var key in appSettings.AllKeys)
                {
                    Console.WriteLine("Key: {0} Value: {1}", key, appSettings[key]);
                }
            }
        }
        catch (ConfigurationErrorsException)
        {
            Console.WriteLine("Error reading app settings");
        }
    }

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