How to read and write custom sections in app.config? - c#

I want to read and write the users settings in the app.config file of my project.
I want to add the user name as a section and adding its setting, for each user if he is not exist in the app.config.
And I want to read it at the beginning of the application.
Can you help me please?

.Net already has this class ConfigurationManager you can use to read app.config and web.config for web scenarios. You could use something like this
var temp = ConfigurationManager.AppSettings["data"];
for more info please check the MSDN ConfigurationManager

Related

C#/ASP.Net: app.settings instead of web.config

I have next scenario:
One Class Library (For example called Utilities) using one app.config
One General Site Web application using web.config and applications with web.config each one.
I need to insert an appsettings called "XXX". obviouslly should set in the Site web.config.
The Utilities library is called in all application that I have inside the Site, then I should set the appsettings key in each web.config (something confusing).
How can I set the key inside app.config and read it? ConfigurationManager only read from Web.Config and I would like to read app.config keys inside Utilites.
Is it possible?
Hi I found the solution:
if yu want to get the properties of app.config you can use:
var value = Properties.Settings.Default.keyName

Store user settings file in executables directory

I'm developing a software in .NET 4.0, which reads and writes application settings. On finish the software will be stored on a file server. No local instances will be executed.
By default the user settings XML file is stored in every users AppData\... directory, but I want to change the file location to the same directory the executable is stored.
This means, that all users should use the same XML user-settings file with modified contents.
I've read this question and answeres where a user describes how to realize that in JSON format.
Question:
Isn't there any other (simple) way to tell the Settings class, where to read from and write to user settings?
The following has been discussed:
Users will always have enough access rights to modify the settings file.
Modifications on settings should be picked up by other users.
Users will start the application from different network computers.
I could implement my own XML file handled by the application (I'll keep this in mind).
I'm not sure if you can get the functionality that you want with the standard Settings class. But I do think that the end result your searching for can be accomplished.
If you want changes in settings user 1 makes to be instantly enforced for user 2, you should look at storing the settings in a database. Your application can then be periodically check this table for changes. For instance if user 1 changes the color of a control, then every time user 2 loads a screen with that control you check the database for the color.
Or, if you want the settings to be applied on start-up of you application. Use a datacontract + xml serializer to write settings to a file of your choosing on a network accessible path/folder. Then make sure you can handle read/write locking of this file.
These are just general ideas that I think you should consider. I dont claim these are your only options though. If you whish to pursue any of these things there are a bunch of blogs and stackoverflow pages with all the information you need.
good luck!
I added a custom Application Configuration File called AppSettings.config to my project and set its Copy to output property to Copy if newer (this copies the config file into the executables folder on building the project.
My settings must be classified as appSettings, not applicationSettings (non-editable) or userSettings (stored in the users AppData folder). Here's an example:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- single values -->
<add key="Name" value="Chris" />
<add key="Age" value="25" />
<!-- lists, semi-colon separated-->
<add key="Hobbies" value="Football;Volleyball;Wakeboarding" />
</appSettings>
</configuration>
Look at the code below, how to access and modify them:
Configuration AppConfiguration =
ConfigurationManager.OpenMappedExeConfiguration(
new ExeConfigurationFileMap {ExeConfigFilename = "AppSettings.config"}, ConfigurationUserLevel.None);
var settings = AppConfiguration.AppSettings.Settings;
// access
var name = settings["Name"].Value;
var age = settings["Age"].Value;
var hobbies = settings["Hobbies"].Value.Split(new[]{';'});
// modify
settings["Age"].Value = "50";
// store (writes the physical file)
AppConfiguration.Save(ConfigurationSaveMode.Modified);
I used the built-in app.config for default application settings and wanted to separate them from the editable global settings in AppSettings.config. That's why I use a custom config file.
That's not directly an answer to the question, but it solves the problem on how to store and share editable application settings in custom configuration files.
Hope it helps any other users! :-)

Dynamically change log target file using Enterprise Logging

So I'm trying to use enterprise logging in an application, and I want it to have multiple files, so far I have the following in my app.config file:
<add name="Normal" fileName="C:\MyApp\Logs.log" .../>
And in my code I'm simply using
Logging.Write("My log here");
Now what if I want to programatically change the file where it's being logged to C:\MyApp\MyDateHere_Logs.log, how could I approach this?
Haven't found a lot of solutions online. Thanks beforehand.
In the end I ended up creating a custom trace listener of my own and then changing the filename attribute on runtime by following the guide here.
You can access the attribute which stores your path by using Attribute["filename"] (assuming the attribute in your config file's trace listener node has the path in an attribute named "filename".)

Using configurationmanager to read from multiple web.config files

Background:
I have some data thats stored in the web.config files of about 100 web applications. This data is getting moved to a database gradually. The webpages will show the web.config data until somebody clicks on an "edit" link in which case they'll be redirected to a webpage which will allow them to update this data where it will be saved in a database instead.
Problem:
Not all of the data will be changed on this page that will save it to the database. When somebody clicks the "edit" link I want the form to populate with the data from the web.config file and when they click "save" have it save to the database. However, using the configurationmanager I can only get it to pull data from the web.config file on current application.
Questions:
Is there a way to use configurationmanager to select the web.config file from lets say ../{dynamic_app_id}/web.config ?
is reading them as plain xml files my only option?
Are there any pitfalls to this approach?
Is there another solution that would work better perhaps?
You can read any config file with ease. Please see my sample code where I read application settings from external app.config file:
System.Configuration.KeyValueConfigurationCollection settings;
System.Configuration.Configuration config;
System.Configuration.ExeConfigurationFileMap configFile = new System.Configuration.ExeConfigurationFileMap();
configFile.ExeConfigFilename = "my_file.config";
config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configFile, System.Configuration.ConfigurationUserLevel.None);
settings = config.AppSettings.Settings;
Happy coding and best regards!
You can add below section in your web.config
then, add "env" folder in your project and add your environmental settings into EnvironmentalSettings.config. And you can still use ConfigurationManager to get settings from EnvironmentalSettings file.
Does that answer your question?

Create asp.net website with editable config file

I want to create setup of my web application with editable web.config file content.
Any help?
If you are trying to change the config with your code, the following should help. Any time you execute the following code, your site will be restarted. So keep that in mind.
var config = (Configuration)WebConfigurationManager.OpenWebConfiguration("~");
var configSection = (MySectionTypeHere)myConfiguration.GetSection("system.web/section");
//make your edits here
myConfiguration.Save();
If this is not what you're looking for, perhaps you could provide some more information.
You could use a tool like MS Web deploy. Read more about it here. http://learn.iis.net/page.aspx/578/package-an-application-for-the-windows-web-application-gallery/

Categories

Resources