reloading app.config after writing - c#

When I use this to write to my app.config file:
Configuration config =ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["Wachtwoord"].Value = "Test";
config.Save();
ConfigurationManager.RefreshSection("appSettings");
I can read it again. But when i close and restart the program, the value of "Wachtwoord" has changed again to the old value.
Does anybody how I could fix this?
Thanks

Are you sure that this has not been caused by Visual Studio overwriting your settings file when you build the project? The original settings file lives with your source code, whereas you run the application from your build output directory (e.g. bin\debug). You may be making changes to the copy in the build output directory when you run the application, which will make changes to the version there. When you rebuild the project, the settings file will be overwritten.

Related

appname.exe.config not created on windows application

When I build a windows application. It does not work because it cannot read my app.config file. I looked in the bin directory and it does not have the appname.exe.config file. If I manually copy over the app.config and rename it the appname.exe.config the application will work..but when I build the project this file is never created automagically. What am I missing? Why is my project not creating it? I have looked through the whole folder structure and there is no other config files.
Everyone here is giving you false information I think. I think #bowlturner had it right. According to Microsoft, you do need to set the app.config's Copy to output directory property to either Copy Always or Copy if newer. From Microsoft's MSDN Site:
When you develop in Visual Studio, place the source configuration file for your app in the project directory and set its Copy To Output Directory property to Copy always or Copy if newer. The name of the configuration file is the name of the app with a .config extension. For example, an app called myApp.exe should have a source configuration file called myApp.exe.config.
Visual Studio automatically copies the source configuration file to the directory where the compiled assembly is placed to create the output configuration file, which is deployed with the app.
The correct settings are:
Build Action = None
Copy to Output Directory = Do not copy
and very important, the project file needs to contain this element:
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
The only thing that worked for me was to delete bin\ and obj\ entirely. Rebuild and the file is created fine.
Look on App.config properties, should be:
BuildAction: None
CopyToOutputDirectory: Do not copy
Custom tool and custom tool namespace should be empty
Also try to Rebuild project. Right click on project -> Rebuild
Assuming you're using Visual Studio click your config file in the Solution Explorer and and in the Properties panel set Copy To Output Directory to something other than Do Not Copy.

Does an application need app.config

Previously sorry for my English...
I want to store connection string in app.config
Does my application need app.config file after it have build
I've tried to delete app.config and then ran application from the release folder but no errors appeared, but when from the debug folder i have got one.
So where app.config's keys embed in application?
By default, when you build; the app.config is renamed to <YourAssembly>.exe.config and copied to the output directory.
This is why it works even if you deleted app.config - the old .config is still in the output directory. Try looking in that directory for a .config file.

Why my changes of AppSettings in App.config is not taken into account in run-time? (Console Application)

I have a console application which has its own App.config.
I need to change some values in section time to time.
My problem is, when I execute the exe within the bin/debug folder it gets the relevant appsettings correctly. But when I edit and change values of some key/value pairs and RE-RUN the exe, it still reads the original values.
(By RE-RUN I mean running the application on the command promt by calling MyTool.exe)
I tried to call
ConfigurationManager.RefreshSection("appSettings");
in the begining of my Main method. But did not help.
Can you please advise?
Thanks
But when I edit and change values of some key/value pairs and RE-RUN
the exe, it still reads the original values.
Depends how you are RE-RUNNing this exe. If you are doing this in Visual Studio, by hitting F5, VS simply copies the app.config file in your project to the output and renames it to AppName.exe.config. So if you want your changes to be taken into account you have to modify AppName.exe.config (not App.config) and then run the executable from the Windows Explorer.
This being said, the App.config is read and parsed only once. When the application starts. The values are then cached to avoid expensive XML parsing everytime your application requests some value.
App.config is designed to store configuration values that are not supposed to be changed. If you need to change configuration values dynamically you should use some other storage mechanism: file, database, ...
But the ConfigurationManager.RefreshSection("appSettings"); method should work. Once you have modified the AppName.exe.config file, you call this method and then refetch the value you need using ConfigurationManager.AppSettings["someKey"]; which should return you the new value.
For me I had to manually delete the application.exe.config located in /obj/Debug/ as this was basically acting like a cache. So anytime I would edit my app.config, (re)build, but looked at the application.exe.config, it was not fully updated after the build. Doing the above allowed me to force the IDE to recreate the application.exe.config in the /bin/
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// change ConnectionString in App.Config for Entity FrameWork Object....
//.....
config.Save();
are you save config file?

Force rebuild after Spring config file edit

In our C# project we use Spring as an Inversion of Control (IoC) container. We noticed, that changes to the Spring configuration xml file do not cause the solution to be rebuild. Therefore the changed configuration is not copied to the output folder and every debug-run uses the old configuration.
How can we force Visual Studio (2008) to copy the config file even though none of the project code has changed?
Further info:
The build action of the config file is set to None. Copy to Output Directory is set to Copy always.
Seems the solution is simpler than I thought. If the build action for the *.xml config file is changed to Embedded Resource, then changing the file also touches the project. Therefore, every debug cycle causes a rebuild of the project, where the config file is included. This causes the file to be copied to the output directory with the adjusted configuration. => Problem solved.

.NET C# executable is not reading config dynamically

In my app.config i got something like:
<appSettings configSource="AppSettings.config"/>
I would have expected the application to read the settings dynamically from AppSettings.config but i doesn't...
Am i wrong here?
app.config is read once at startup. Re-reading each time a config value is referenced could be a big performance hit. Besides, there are some entries like dependencies that it wouldn't make sense to change at runtime.
You are using the wrong attribute, use the file attribute to reference an external configuration file:
<appSettings file="AppSettings.config"/>
The Setting is correct. But as ctford said, this is only read once, when the application starts up... Is the file in the same folder as the application executable is being loaded from ? Also, in Visual Studio, where you have the file stored, right click on it, and verify that you have the Visual Studio File property "Copy to Output Directory" set appropriately. For files like this, the default is "Do Not Copy" and then the file will, duh, not be there when the app runs and looks for it...

Categories

Resources