I understand you want to save application settings and user settings. This in contrast to a file: one operator may want to create several different files, but his settings remain the same until they are changed, similar like options are changed.
If this is what you are looking for, then the method you can use depends on how many application and user settings you are talking about.
Method 1: Properties.Settings
If you don't have a lot, say less then 30 or something, maybe 50, then the easiest would be to use the application settings of your project.
- In solution explorer find the project where the application and user setting are used
- Right click on it and select properties
- In the new window, on the left you'll see Application / Build / Build Events / etc.
- Find settings
- Here you can give the setting a name, a type (string / int / datetime / etc) and a default value.
- Application settings are usually not changeable, user settings are. Use the scope to select between application and user
Once you've typed all your settings and saved them. The environment has created for you a class that is derived from System.Configuration.ApplicationSettingsBase. This class has properties for all your settings. Access as follows:
Properties.Settings.Default.MySetting = ...
var myOtherSetting = Properties.Settings.Default.MyOtherSetting;
Properties.Settings.Default.Save();
You can Save / Reload / Reset these properties and get notified whenever the properties change. See ApplicationSettingsBase
The user properties are saved per user and automatically loaded when your program starts.
Method 2: Create a settings class
Another possibility is that you create a class with properties for all your settings and load / save them when needed, for instance when the program starts or exist.
Saving them in XML format is easy when you use the XMLFormatter class.
private void SaveAsXML(MySettings settings, string fileName)
{
using (var streamWriter = File.CreateText(fileName))
{
var xmlSerializer = new XMLSerializer(typeof(MySettings));
xmlSerializer.Serialize(streamWriter, settings);
}
}
private MySettings ReadAsXML(string fileName)
{
using (var streamReader = File.OpenText(fileName))
{
var xmlSerializer = new XMLSerializer(typeof(MySettings));
return xmlSerializer.Deserialize(streamReader);
}
}
Advantage: full control over your settings. You can hand over the settings from one operator to another operator.
Disadvantage: you have to take care that they are saved in the proper places