C# 参数 class 和 app.config
C# Parameters class with app.config
我有一个配置 class,其中包含我的应用程序的所有参数,用于从扫描仪获取图像。
我有 color/BW、分辨率...
等参数
参数经常更改,所以我正在寻找一种解决方案,当我将更改后的参数保存在 app.config 文件中时自动写入。并做恢复的事情,从 app.config 中写入我的 class 在软件的初始化。
这是我的两个 classes :
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 :
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; }
}
问题可以转化为如何将对象保存到某种持久性中?
要么使用数据库(似乎有点矫枉过正),要么使用序列化程序对其进行序列化,要么自己将其全部写入文本文件。使用 json 序列化,序列化您的 ScannerParameters 然后将其写入文件似乎是最合适的。
使用 newtonsoft json,这是 .net 的实际标准,有很好的示例 @ http://www.newtonsoft.com/json/help/html/SerializingJSON.htm
在你的情况下你会这样做:
// our dummy scannerParameters objects
var parameters = new ScannerParameters();
// let's serialize it all into one string
string output = JsonConvert.SerializeObject(paramaters);
// let's write all that into a settings text file
System.IO.File.WriteAllText("parameters.txt", output);
// let's read the file next time we need it
string parametersJson = System.IO.File.ReadAllText("parameters.txt);
// let's deserialize the parametersJson
ScannerParameters scannerParameters = JsonConvert.DeserializeObject<ScannerParameters>(parametersJson);
我有一个配置 class,其中包含我的应用程序的所有参数,用于从扫描仪获取图像。
我有 color/BW、分辨率...
等参数
参数经常更改,所以我正在寻找一种解决方案,当我将更改后的参数保存在 app.config 文件中时自动写入。并做恢复的事情,从 app.config 中写入我的 class 在软件的初始化。
这是我的两个 classes :
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 :
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; }
}
问题可以转化为如何将对象保存到某种持久性中?
要么使用数据库(似乎有点矫枉过正),要么使用序列化程序对其进行序列化,要么自己将其全部写入文本文件。使用 json 序列化,序列化您的 ScannerParameters 然后将其写入文件似乎是最合适的。
使用 newtonsoft json,这是 .net 的实际标准,有很好的示例 @ http://www.newtonsoft.com/json/help/html/SerializingJSON.htm
在你的情况下你会这样做:
// our dummy scannerParameters objects
var parameters = new ScannerParameters();
// let's serialize it all into one string
string output = JsonConvert.SerializeObject(paramaters);
// let's write all that into a settings text file
System.IO.File.WriteAllText("parameters.txt", output);
// let's read the file next time we need it
string parametersJson = System.IO.File.ReadAllText("parameters.txt);
// let's deserialize the parametersJson
ScannerParameters scannerParameters = JsonConvert.DeserializeObject<ScannerParameters>(parametersJson);