reading from app.config file

后端 未结 8 2353
不知归路
不知归路 2020-11-29 19:34

I am trying to read StartingMonthColumn and CategoryHeadingColumn from the below app.config file using the code

ConfigurationSettings.AppSettings[\"StartingM         


        
相关标签:
8条回答
  • 2020-11-29 19:57

    The reason is simple, your call to ConfigurationSettings.AppSettings is not returning the required config file. Please try any of the following ways:

    • Make sure your app config has the same name as your application's exe file - with the extension .config appended eg MyApp.exe.config
    • OR you can use ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location).AppSettings["StartingMonthColumn"]

    Hope this helps

    0 讨论(0)
  • 2020-11-29 19:58

    Just for the future reference, you just need to add System.Configuration to your references library:

    0 讨论(0)
  • 2020-11-29 20:00

    ConfigurationSettings.AppSettings is obsolete, you should use ConfigurationManager.AppSettings instead (you will need to add a reference to System.Configuration)

    int value = Int32.Parse(ConfigurationManager.AppSettings["StartingMonthColumn"]);
    

    If you still have problems reading in your app settings then check that your app.config file is named correctly. Specifically, it should be named according to the executing assembly i.e. MyApp.exe.config, and should reside in the same directory as MyApp.exe.

    0 讨论(0)
  • 2020-11-29 20:00

    Try:

    string value = ConfigurationManager.AppSettings[key];
    

    For more details check: Reading Keys from App.Config

    0 讨论(0)
  • 2020-11-29 20:06

    Also add the key "StartingMonthColumn" in App.config that you run application from, for example in the App.config of the test project.

    0 讨论(0)
  • 2020-11-29 20:12

    Try to rebuild your project - It copies the content of App.config to "<YourProjectName.exe>.config" in the build library.

    0 讨论(0)
提交回复
热议问题