It's been a long time since I don´t write anything. I've been a little bit busy working and jumping from Silverlight to Windows Services, Installers, Telerik Controls in ASP.NET, etc.
While working, I've found out an interesting challenge. How to access another's application app.config file and be able to read and write to it?
Think in this scenario for a second, you have a Windows Service (not UI if you are not familiar with Windows Services) that needs to read some parameters from the configuration file. And the configuration is really complex that you prefer to have a Windows Form Application to help you set all the values.
That's exactly what I want to accomplish. So here we go. You'll need to add System.Configuration to your project's reference.
To read a value from the app.config it's the basic thing and it's very easy:
To write a value it's not so easy, at least it doesn't work the other way around. You cannot do something like:
ConfigurationManager.AppSettings["key"] = "this is wrong" <- WRONG!
While working, I've found out an interesting challenge. How to access another's application app.config file and be able to read and write to it?
Think in this scenario for a second, you have a Windows Service (not UI if you are not familiar with Windows Services) that needs to read some parameters from the configuration file. And the configuration is really complex that you prefer to have a Windows Form Application to help you set all the values.
That's exactly what I want to accomplish. So here we go. You'll need to add System.Configuration to your project's reference.
To read a value from the app.config it's the basic thing and it's very easy:
string value = ConfigurationManager.AppSetttings["key"];To write a value it's not so easy, at least it doesn't work the other way around. You cannot do something like:
ConfigurationManager.AppSettings["key"] = "this is wrong" <- WRONG!
This is how you do it:
// Open App.Config
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Add("date", DateTime.Now.ToShortString());
// Update an Application Setting.
config.AppSettings.Settings["key"] = "this is correct";
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
In order to access another's application app.config you need to know the file location. For the problem I have exposed I'm writing the Windows Service location to Registry using System.Reflection.Assembly.GetExecutingAssembly().Location. Then in my configuration application I retrieve this value and use it this way:
ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();
filemap.ExeConfigFilename = Location; //(this is the location of the app.config I'm trying to access)
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
AppSettingsSection appsettings = config.AppSettings;
appsettings.Settings["key"] = "value";
config.Save();
So now we are able to access the app.config, read and write to it even if it's another's application config file.
Hope this is worthy!
Important Note:
When debugging, you won't get anything written in your app.config.
Actually if you "Quick Watch" your config variable you will notice that is pointing to a file named something like: ApplicationName.vshost.exe.config.
Don't worry I can assure you this code works!

No hay comentarios:
Publicar un comentario