i'm throwing an "Object reference not set to an instance of an object." when i try to write to the appSettings part of app.config.
here's my sample code:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["ComPort"].Value = "COM4";//exception thrown here
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
i've also tried
config.AppSettings.Settings.Add("ComPort","COM4");this doesn't throw any errors, but when it runs it seems to do nothing at all.
any ideas?

2 answers
I think there must be a problem with your app.config file to get this error. This is basically how it should look for your code to work:
In particular, I'd check that 'appSettings' is a child of 'configuration' and not of something else as I got a null reference exception myself when I changed it to:
As you may know, app.config is copied to your bin/debug or bin/release folder when the project is built and 'app' is replaced by the actual name of the executable (e.g. myapp.exe.config). So, if there is a problem, that will need changing as well.
answered 7 months ago by:
12813
237
this is what my code looks like: <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="ComPort" value=""/> </appSettings> </configuration> i know it doesn't have the "COM1" in it, but if it adds it, then it works, right?
237
wtf? newline key doesn't work in comments? [code][/code] doesn't work either?
237
sorry vulpes, i tried copy/pasting your app.config file and it didn't work. i added- <add key = "ComPort value"COM1"/> and tried to change to COM4, and that didn't work either. tried both lines also -- config.AppSettings.Settings["ComPort"].Value = "COM4"; and config.AppSettings.Settings.Add("ComPort","COM4"); neither worked
237
i get a ---use the "new" keyword to create an object instance error
so, apparently, you can't modify the app.config file directly through the debugger, you need to run the .exe in your bin folder, then it'll modify the file. if i run the .exe file in the bin folder, it will modify the .config file as it should with the above code. any ideas on how to use the debugger to modify the .config file in visual studio?
answered 7 months ago by:
237
12813
Although it's treated specially by VS, app.config is just an ordinary XML file which you can edit with any text editor or programatically. If you alter app.config at design time (by double clicking it in Solution Explorer) then, when the solution is rebuilt, the copy in the bin folder will be altered accordingly. To test this theory, I changed the ComPort setting to an empty string and it still worked fine. However, when you change the setting programatically, only the copy in the bin folder gets changed - app.config itself is unaltered. If you wanted to change the contents of the file 'manually' whilst debugging, then I think you'd need to keep a notepad window open. However, the file will of course be locked when the ConfigurationManager is writing to it and possibly also when it's reading from it so I'm not sure that it's such a great idea.