类我有我的应用程序的所有参数的配置类,从扫描仪获取图像。
我有喜欢的颜色/ BW,分辨率...
的参数经常更改的参数,所以我在寻找,当我保存的参数改变的参数在app.config文件的解决方案自动写入。为了完成恢复的事情,请在软件初始化时从app.config编写我的类。 这里是我的两个类:C#参数用的app.config
private void GetParameters() {
try
{
var appSettings = ConfigurationManager.AppSettings;
Console.WriteLine(ConfigurationManager.AppSettings["MyKey"]);
if (appSettings.Count == 0)
{
Console.WriteLine("AppSettings is empty.");
}
else
{
foreach (var key in appSettings.AllKeys)
{
Console.WriteLine("Key: {0} Value: {1}", key, appSettings[key]);
}
}
}
catch (ConfigurationErrorsException)
{
MessageBox.Show("Error reading app settings");
}
}
private void SetParameters(string key, string value)
{
try
{
Configuration configManager = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection confCollection = configManager.AppSettings.Settings;
if (confCollection[key] == null)
{
confCollection.Add(key, value);
}
else
{
confCollection[key].Value = value;
}
configManager.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(configManager.AppSettings.SectionInformation.Name);
}
catch (ConfigurationErrorsException)
{
MessageBox.Show("Error writing app settings");
}
}
我不想调用的方法每一个参数...
还有就是我的参数类:
class ScannerParameters
{
public bool Color { get; set; }
public int Resolution{ get; set; }
public string FilePath { get; set; }
public TypeScan TypeScan { get; set; }
public string TextTest{ get; set; }
}
所以你的意思是,如果有人改变了你的应用程序中的参数,你希望这些值保存回配置? –
这就是我正在搜索的行为。 – betsou
它只是不工作?你真的不说,麻烦的是什么,所以这是一个有点不清楚你需要什么.. –