Changeable config values - c#

I need to store few config values which will be changed by the application administrator. I do not want to store them in web.config files, because whenever web.config file is edited the system will recompile the application.
DBA is not letting me to create a table to store it in database.
Can you suggest some way where I can store this editable values.
Thank you for the suggestion.
it is a ASP.NET application.

You could store them in your own config file. For example you could put this file inside the ~/App_Data folder and then read the config file when you need the values. As far as the format of this file is concerned you have many choices: plain Text, XML, JSON, whatever you like.

As an alternative you could write your own ConfigSection and set restartOnExternalChanges="false".
Then, when reading the section with ConfigurationManager.GetSection("yourSection") the settings will be auto-refreshed without an application restart.
If you still wish to keep web.config untouched, you can use configSource property to set external file where your settings will be kept.
To design a new section you can use this tool: http://csd.codeplex.com/

Related

Settings For Individual Programmers In A Config File

I'm wondering if there is a way to create settings in a config file for individual programmers. The situation I'm encountering is that there are some programmers who want settings turned on and several that want them turned off. Our config files are in SVN source control, so using a shared config file means we are always overwriting each others settings. We are doing this for an ASP.NET web application project. My initial thoughts would be to create a config file outside of source control, but how do I make it so that each programmer has his own copy?
Here is some further clarification. We have a link in the main web.config file that points to an environment-based file (e.g. file used for dev, staging and live).
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings configSource="EnvironmentConfigs\appSettings.config" />
...
</configuration>
Inside the appSettings.config file, we have settings for dev, staging and live. What I'd like to do is create another config file called user.config and have the individual programmer settings staored there (not in source control of course). What do I need to do in order to have visual studio read from this new file?
The settings file does not need to be versioned in SVN, or each developer can select their settings file to not be overwritten or committed. See the SVN settings file, it could even be added as a global ignore.
Each developer can set up their own build congifuration in Visual Studio. This build configuration should be linked to a build script, which replaces sections of the Web.Config/App.Config/*.Config files specifically as the developer wants.
This way, when they want to make changes to the config files to suit themselves, they should change the replacements in the build script rather than changing the config file directly.
This article covers some of the points i've mentioned: http://www.diaryofaninja.com/blog/2010/05/09/automated-site-deployments-with-teamcity-deployment-projects-amp-svn
Remove the config file from source control and set the svn:ignore property on it. Then it won't be committed. Then, also create another file, like, Production.web.config, that has the production values in it, so you still keep those around too.
That's what I do!
Don't commit config file changes unless they are new settings that to be sent to everyone.
Otherwise, checkout, edit, and leave it checked out. You'll know if someone else has added/modified the file when you get latest version. At which time you merge your settings with their changes, but leave it checked out on your machine.
We do not store application configuration files in source control. Instead, in source control, we store a configuration file template, usually named something like web.config.template. Each developer has their own 'values' file, usually named web.config.values-bem for instance. Each developer also sets up a post-commit hook which takes the template file, and processes it, replacing 'variables' with their values from the specified values file.
For instance, my config values file has the following definition in it:
DB_SERVER=.\SQLEXPRESS
In the web.config.template file, this exists:
connectionString="server=#DB_SERVER#[DEV1];Persist Security Info=True;Password=#DB_PW#;User ID=#DB_USER#;database=#DB_NAME#;Enlist=false;Max Pool Size=100" />
So when the process runs (a python script), it replaces all instances of #DB_SERVER# with the setting I have in my values file. The template script allows for default values to be specified right in the template as well, so you can make changes to the template file and not break other developers' environments (usually). (The default values are next to the variable, in square brackets.)
This solution allows each developer to have their own settings, but still have a web.config.template file that's versioned, and each developer avoids 'inflicting' configuration changes on other developers.
This works well for us. If you want to use the same scheme, you can check out the code for it on my github: https://github.com/bmontgomery/FileReplace. I can help you with the hook scripts as well if you're interested in that.

save application configurations

Is there a way to save application configurations and settings that user have customized in a way like we use the app.config file?
the app.config file is read-only so I cannot add keys or edit values in it.
I want something easy to use that have add or edit keys and values just like app.config.
users customize their settings in visual forms and then the program should store it.
Use ConfigurationManager in System.Configuration namespace
The normal app.config is only read-only to the normal user, but a shadow is written to the user's data folders when you save a User setting.
But you may have to expand on "Add keys".
Isolated storage can be used for this purpose:
http://msdn.microsoft.com/en-us/library/cc221360(v=vs.95).aspx

Overwrite app.config with the default one

How can I overwrite the app.config file form the App Data folder with the default one by code?
Not sure what you mean but you can use at the configurationSettings class to manage settings, you can also use a simple Data.XML namespace and change the XML in the config file and save it back but just beware that it is not the recommanded.
typically i have always created an config object that i read all the config file settings and use it as a singleton in my code. if you want you can add a save method to the class that saves back the config file if you want to persist the changes that your code makes to the config class.
You can use a settings file. The VS designer will generate a proxy class for you. For each entry you can specify in the properties window wether you want to "GenerateDefaultInCode" or not. This way, you can configure your settings in the app.config file under the "ApplicationsSettings" section and at the same time rely on default values (set via attributes in the proxy class) in the generated code.
See http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx for how to add a .settings file to your project.
As pointed out here default values should be used with care because they can lead to error-prone behaviour when you deploy to a different environment.

Saving user configuration options

I am creating a winform application in .net 2 with c#. I need to be able to save user configuration data and I am considering using an XML file for this propose. What is the general feeling for saving user configuration data? I have read that it is not in vogue to write to the registry but rather to a file instead. Please write your thoughts.
One option is to add it to settings file. In Visual Studio, go to My Project -> Settings and add it there. You can access it this way: C# - properties.settings.default...; VB - My.Settings...
Adding a setting to settings file stores the value in app config file and auto generates a class property for easy access to the value.
Use the ConfigurationManager class to store the settings.

How can I store user-tweakable configuration in app.config?

I know it is a good idea to store configuration data in app.config (e.g. database connection strings) instead of hardcoing it, even if I am writing an application just for myself. But is there a way to update the configuration data stored in app.config from the program that is using it?
If you use the Settings for the project, you can mark each setting as either application or user.
If they're set as user, they will be stored per-user and when you call the Save method it will be updated in the config for that user.
Code project has a really detailed article on saving all types of settings.
app.config isn't what you want to use for user-tweakable data, as it'll be stored somewhere in Program Files (which the user shouldn't have write permissions to). Instead, settings marked with a UserScopedSettingAttribute will end up in a user-scoped .config file somewhere in %LocalAppData%.
I found the best way to learn this stuff was to mess with the Visual Studio "Settings" tab (on your project's property pages), then look at the code that it generates and look in %LocalAppData% to see the file that it generates.

Categories