【问题标题】:Saving Complex User Settings in .NET C#在 .NET C# 中保存复杂的用户设置
【发布时间】:2013-11-28 08:56:00
【问题描述】:

我正在编写一个小工具来连接到 MS SCOM 服务器并添加订阅以监控警报。但是,我想在某个地方/某种方式保存连接和订阅设置。这是粗略的结构

public class ConnectionManager
{
    ...
    public BindingList<Connection> Connections // All the connection we know
    ...
}

然后我们就有了联系。基本上这个类保存所需的连接字段,如服务器名称、用户域、连接到服务器、保持连接活动等

public class Connection
{
    ...
    public string Name;                               // the name of this connection
    public string ServerName;                         // the scom server name
    public string UserDomain;                         // the domain, NETBIOS or FQDN
    public BindingList<Subscription> Subscriptions;   // The subscriptions for this connection
    ...
}

最后,连接类具有警报标准。警报标准基本上是某种正则表达式。目前只是“扩展”(只是一个字符串),但我将构建简单的表达式,这些表达式将即时构建。它看起来像这样

public class Subscription
{
    ...
    public string ExtededExpression;                        // if not empty, use this
    public BindingList<CriteriaOperator> SimpleOperators;   // otherwhise build a regular expression from this stuff
    ...
}

与另一个子类

public class CriteriaOperator
{
    ...
    public string Field;                  // The field, g.g. LastModified
    public LogicalOperator Operator;      // The logical operator, e.g. LIKE
    public string CompareFieldTo;         // A regular expression, e.g. "%SQL%"
    ...
}

现在我的问题是:您认为保存这种结构的最佳方法是什么?我是 .NET 的新手,所以我不确定:使用应用程序设置?或者使用 XML 序列化器序列化/反序列化这个结构?或者编写一些包装类,然后使用 XML 序列化器?或者是否有完全不同的方法?

感谢您的帮助!

【问题讨论】:

  • 我认为最好的方法是创建自己的自定义配置部分msdn.microsoft.com/en-us/library/2tw134k3.aspx
  • 真的取决于场景。我不同意 Yevgeniy 关于使用配置部分的观点,因为这听起来可能是“公关用户设置”……(我知道您可能只有一个用户公关安装,但仍然如此)……序列化通常是一种不错的方法国际海事组织。

标签: c# .net application-settings


【解决方案1】:

我建议不要使用 ApplicationSettings。恕我直言 ApplicationsSettings 用于存储和重新加载您的 UI 状态,例如在文本编辑器中使用的最后 10 个文件或特定用户的“不再显示此消息”!

如果您正在考虑加载/保存方案,我建议您创建一个类来存储保存应用程序状态所需的所有字段。 不要使用序列化直接存储您的某些逻辑类,这会搞砸事情并降低您的可维护性。

通过使用单个保存项,您可以将所有加载/存储操作集中在一个地方。

所以...

public class SaveItem
{
    // ----------------------------------------------
    // version 1.02:
    public bool New { get; set; }
    public string VeryNew { get; set; }

    // ----------------------------------------------
    // version 1.01:

    public bool Bugfix { get; set; }
    public List<string> AList { get; set; } 

    public SaveItem()
    {
        // ...
    }

    public bool TrySave(string fullFilePath)
    {
        bool result = false;
        TextWriter textWriter = new StreamWriter(fullFilePath);
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(SaveItem));
        try
        {
            xmlSerializer.Serialize(textWriter, this);
            result = true;
        }
        catch (IOException)
        {

        }
        finally
        {
            try
            {
                textWriter.Close();
            }
            catch
            { }
        }
        return result;
    }


    public static bool TryLoad(string fullFilePath, out SaveItem saveItem)
    {
        bool result = false;
        saveItem = new SaveItem();
        TextReader textReader = null;
        try
        {
            textReader = new StreamReader(fullFilePath);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(SaveItem));
            saveItem = (SaveItem)xmlSerializer.Deserialize(textReader);
            if (saveItem != null)
            {
                result = true;
            }
        }
        catch (FileNotFoundException)
        {
            if (saveItem != null)
            {
            }
        }
        finally
        {
            if (textReader != null)
            {
                textReader.Close();
            }
        }
        return result;
    }

}

【讨论】:

  • 这就是我故意要这样做的。现在,我编写了一些包装类并对它们进行反序列化,以保存和加载数据结构。工作正常。
【解决方案2】:

至于我,为此你应该:

  • 编写一个 singleton 类(here 是我博客上的一篇很棒的文章)来处理设置。
  • 使用二进制或 XML 序列化 - 因为它很容易且可自定义
  • 保存设置到通用路径,我已经回答了where to store it

【讨论】:

  • 感谢用户文件夹的支持!工作正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-13
  • 2011-03-03
  • 2020-12-07
  • 2013-05-05
  • 2023-03-27
  • 2021-11-04
相关资源
最近更新 更多