0

I'm using System.Configuration.ConfigurationManager.AppSettings.["...."]. However, I found that the newly added <add key="new1" value="...." /> always got null values when execute System.Configuration.ConfigurationManager.AppSettings.["new1"] (the order in app.config file doesn't matter).

Visual studio debugger shows that System.Configuration.ConfigurationManager.AppSettings.AllKeys has the type of string[8]. Why it only get 8 items only?

ca9163d9
  • 27,283
  • 64
  • 210
  • 413
  • That's not a type, that's how many items are in the array. The array's type is `string[]`. Also make sure that the config file *does* exists. It should be named "MyAppName.exe.config" not "app.config". If it doesn't you'll get null values as discussed [here](http://stackoverflow.com/questions/10766654/appsettings-get-value-from-config-file) – Panagiotis Kanavos Nov 10 '14 at 16:30
  • I'm running it in Visual studio. I have 10 items. I found that the `Bin\Debug\....exe.config` has only 8 items. Why Visual studio doesn't update the file by updated app.config? – ca9163d9 Nov 10 '14 at 16:32
  • Even stranger. I cleaned the project and rebuilt it. However, it created xxx.exe.config with `Date modified` of 7/21/2014.... and there is no appsettings session now. – ca9163d9 Nov 10 '14 at 16:39
  • Visual Studio will copy your project's `app.config` to `YourAppName.exe.config` in the `Debug` folder as it should when you build or debug your project. Does your project have an `app.config` file or did you modify the file in the `Debug` folder directly? How did you add the `app.config` to your project? In which file did you add the `appsettings` section? – Panagiotis Kanavos Nov 10 '14 at 17:13
  • I have a `app.config` and added appsettings there. I never modify the file in Debug folder directly. I add the file using Visual studio. – ca9163d9 Nov 10 '14 at 17:16
  • Well, which appConfigs are missing? – Christopher Stevenson Nov 10 '14 at 20:10
  • I may manually copied the file myself. Visual studio may not automatically update the file for F# project. – ca9163d9 Nov 11 '14 at 05:02
  • Do you have both a bin\Debug and bin\Release folder? If so, what is the time stamp on the xxx.exe.config file in the bin\Release folder? – Wallace Kelly Nov 11 '14 at 19:55
  • @Wally Yes, I have both the folders. I just checked and found that the xxx.exe.config in bin\Release folder has the same time stamp of file app.config. It seems Visual studio correctly copied the file for "Release". – ca9163d9 Nov 11 '14 at 20:58

1 Answers1

0

It sounds like Visual Studio is doing a Release build into the bin\Release folder and not updating the contents of bin\Debug.

The default Visual Studio projects include two "configurations" -- Debug and Release. If Visual Studio is set to do a Debug build, it writes into the bin\Debug folder by default. If Visual Studio is set to do a Release build, it writes into the bin\Release folder. All this stuff is customizable for projects and solutions.

To switch between "Configurations", look for the drop-down-box in the toolbar that says, "Release". If you change that to "Debug" and perform a rebuild, you will find that the xxx.exe.config in the bin\Debug folder is correctly overwritten with the updated app settings.

By the way, Visual Studio has a "Batch Build" feature that can be used to build both Debug and Release builds. It is found under the Build menu item.

Wallace Kelly
  • 15,565
  • 8
  • 50
  • 71
  • I understand the default two configurations - _Debug_ and _Release_. And I did switch both of the configuration for testing. The question is why Visual studio doesn't create a new xxx.exe.config file based on app.config but stead it creates a barebone xxx.exe.config in _Debug_ configuration. – ca9163d9 Nov 12 '14 at 15:23
  • I would suggest shutting down Visual Studio. Deleting the bin folder. Restarting Visual Studio. Perform both a Debug and Release build. What do you observe? – Wallace Kelly Nov 12 '14 at 18:05
  • I cleaned the solution (which deleted everything in the Bin\Debug and Bin\Release folders) and rebuilt both Debug and Release release. The Bin\Debug\xxxx.exe.config is the barebone file with timestamp of the project creation time while the Bin\Release\xxx.exe.config is updated with app.config and they have the same time stamp. – ca9163d9 Nov 12 '14 at 19:43
  • I just tested it. Cleaning a solution only deletes the contents of the currently selected configuration. I would suggest shutting down Visual Studio. Deleting the bin *and obj* folders. Restarting Visual Studio. Perform both a Debug and Release build. What do you observe? – Wallace Kelly Nov 12 '14 at 20:22