C# 带有app.config的C参数类

C# 带有app.config的C参数类,c#,app-config,C#,App Config,我有一个包含应用程序所有参数的配置类,用于从扫描仪获取图像。 我有一些参数,比如颜色/BW,分辨率。。。 参数经常更改,因此我正在搜索一个解决方案,以便在将更改后的参数保存在app.config文件中时自动写入。要执行还原的操作,请在软件的init处从app.config编写我的类。 以下是我的两门课: private void GetParameters() { try { var appSettings = ConfigurationM

我有一个包含应用程序所有参数的配置类,用于从扫描仪获取图像。 我有一些参数,比如颜色/BW,分辨率。。。 参数经常更改,因此我正在搜索一个解决方案,以便在将更改后的参数保存在app.config文件中时自动写入。要执行还原的操作,请在软件的init处从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; }

}

这个问题可以转化为如何将对象保存为某种持久性

要么使用一个数据库似乎是一种过度使用,要么使用序列化程序对其进行序列化,要么自己将其全部写入一个文本文件。使用json序列化,序列化ScannerParameters,然后将其写入文件似乎是最合适的

使用newtonsoft json,这是.net的实际标准,有很好的例子@

在您的情况下,您会:

// 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);

那么你的意思是,如果有人更改了你应用程序中的参数,你希望这些值保存回配置?这正是我正在搜索的行为。它只是不起作用吗?你没有说问题出在哪里,所以不清楚你需要什么..App.config不是设计用来写的。如果要持久化数据,请使用数据库、json文件或其他任何东西。问题可以转化为如何将对象保存到某种持久化中?要么使用数据库,要么使用某种序列化程序对其进行序列化,要么自己将其全部写入文本文件。使用json序列化,序列化ScannerParameters,然后将其写入文件似乎是最合适的。为什么是.txt文件而不是.json文件?参数是路径,毕竟json只是文本。json似乎比file.txt更可怕。我想这只是我个人的喜好。的确,调用thing file.json更具描述性。谢谢,这就是我使用的解决方案。如何将其反序列化为ScannerParameters类?绝对没有理由不将其写入app.config,因为用户最初要求提供有关的帮助。序列化到一个文件(无论是Json还是XML)是一个选项,尤其是当他试图存储一个复杂对象时。但对于简单的钥匙来说,这是过分的。