【问题标题】:Techniques for sharing a config between two apps?在两个应用程序之间共享配置的技术?
【发布时间】:2010-12-13 23:15:38
【问题描述】:

我的应用程序分为一个配置工具,它编写配置和一个查看器,它只读取和使用配置中的设置。

在这种情况下会推荐哪些存储属性的技术?不同类别的 XML 是个好主意吗?

这些应用程序使用 C#、.NET 3.5 并使用 WinForms 开发。

【问题讨论】:

    标签: c# .net configuration


    【解决方案1】:

    我会有一个共享程序集,其中包含您的设置类。然后,您可以将此类序列化/反序列化到硬盘上的一个公共位置:

    [XmlRoot()]
    public class Settings
    {
        private static Settings instance = new Settings();
    
        private Settings() {}
    
        /// <summary>
        /// Access the Singleton instance
        /// </summary>
        [XmlElement]
        public static Settings Instance
        {
            get
            {
                return instance;
            }
        }
    
        /// <summary>
        /// Gets or sets the height.
        /// </summary>
        /// <value>The height.</value>
        [XmlAttribute]
        public int Height { get; set; }
    
        /// <summary>
        /// Main window status (Maximized or not)
        /// </summary>
        [XmlAttribute]
        public FormWindowState WindowState
        {
            get;
            set;
        }
    
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Settings"/> is offline.
        /// </summary>
        /// <value><c>true</c> if offline; otherwise, <c>false</c>.</value>
        [XmlAttribute]
        public bool IsSomething
        {
            get;
            set;
        }
    
        /// <summary>
        /// Save setting into file
        /// </summary>
        public static void Serialize()
        {
            // Create the directory
            if (!Directory.Exists(AppTmpFolder))
            {
                Directory.CreateDirectory(AppTmpFolder);
            }
    
            using (TextWriter writer = new StreamWriter(SettingsFilePath))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                serializer.Serialize(writer, Settings.Instance);
            }
        }
    
        /// <summary>
        /// Load setting from file
        /// </summary>
        public static void Deserialize()
        {
            if (!File.Exists(SettingsFilePath))
            {
                // Can't find saved settings, using default vales
                SetDefaults();
                return;
            }
            try
            {
                using (XmlReader reader = XmlReader.Create(SettingsFilePath))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                    if (serializer.CanDeserialize(reader))
                    {
                        Settings.instance = serializer.Deserialize(reader) as Settings;
                    }
                }
            }
            catch (System.Exception)
            {
                // Failed to load some data, leave the settings to default
                SetDefaults();
            }
        }
    }
    

    您的 xml 文件将如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <Settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Height="738" WindowState="Maximized" IsSomething="false" >
    </Settings>
    

    【讨论】:

    • 好主意,谢谢。例如,我如何序列化复选框的状态?
    • 添加一个布尔属性 IsFooChecked。加载表单时,调用 Settings.Instance.Deserialize(),然后将控件的 Checked 属性设置为 IsFooChecked。在 OnFormClosing 中,将 IsFooChecked 设置为复选框的 Checked 属性,然后调用 Settings.Instance.Serialize()。
    • 非常感谢,是否还有一种方法可以改进该类以制作类别而不是将所有属性存储在一个标签中?
    • 您可以将属性分组到类中(每个类别一个类)。例如,FooClass。然后,您将在 Settings 类中拥有一个名为 Foo 的属性,其类型为 FooClass - 具有 XmlElement 属性。
    • 新类:public class Appearance { public bool FullscreenMode {get;set;} public bool TrayTooltipActive {get;set;} public Appearance() { } } 在设置类中:[XmlElement] public Appearance View { 得到;放;访问方式:Util.Settings.Instance.View.FullscreenMode = false; Util.Settings.Instance.View.TrayTooltipActive = true;
    【解决方案2】:

    XML 似乎是这方面的理想选择。

    在 WinForms 中,用户设置通过 XML 持久化,因此您拥有所需的所有类和辅助方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 2012-01-17
      • 2018-04-19
      • 2010-11-17
      • 2011-12-15
      • 2021-04-12
      • 1970-01-01
      相关资源
      最近更新 更多