save application configurations - c#

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

Related

Changeable config values

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/

How to go about creating a configuration file for my application in .NET

I am working on a software and want to create a configuration file for my application to store configurations that will be used at runtime. I have seen some software use a config.xml file to achieve this. The configuration file I want to use will have:
Cache folder location
Color scheme
Option to toggle caching
How to go about this? I am working with WPF.
In Visual Studio, add an "app.config" to your project. When you build your application, this will create an AppName.exe.config file, where AppName is the name of your executable. This is an XML config file that can contain your settings.
If theses options are user-specific, don't use app.config - that's for global application settings and you will clobber other users' settings. I'm bringing this point up because you mentioned caching options in your question, and in some applications, the choice to cache or not to cache is at the user level.
Instead, I would recommend that you create a domain object to store the configuration settings to, and serialize the object to a local folder in the current user's isolated storage folder.
When the application starts up, just look into the current user's isolated storage folder, confirm the serialized file exists, and deserialize back to the domain object.
as you are in WPF world, you also can use App.XAML for defining and using color schemes
Use standard .NET settings framework.
More details here: How is the logic behind storing program setting in text file like 'config.cfg'?
Also this will help http://msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx.
Although this article is a little old, not much changed since then in this realm.

Is appname.exe.config required for user specific settings?

I'm building a simple winforms application which is using built-in settings for saving program options. All of the settings are per-user.
As I can see from the generated cs file the default settings are embedded in the source code but the application still reads the appname.exe.config file when launched. If I delete the file the application seems to be working fine but I want to make sure that it will not break anything.
So is the file required if I am only using per-user settings?
You should be using user settings instead of a configuration file. Refer to this article for more information, including how to read/write user settings.
The settings designer embeds the default value in a [DefaultSettingValue] attribute. The following section in the Remarks section of the attribute is relevant:
Different settings providers may have
different requirements or limitations
on the use of the
DefaultSettingValueAttribute. For
example, the LocalFileSettingsProvider
does not require this attribute, and
will override any value provided by
this attribute if there are any
values—default or user-modified—
already present in the data store.
LocalFileSettingsProvider is the default provider. So, it's there but it doesn't get used.
If you are using the Application Settings and you want persistence of the settings then yes this is where the options are saved. If you delete the file it will simply recreate it if the user changes a setting and you are setting the setting.

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