技术的两个应用程序之间共享的配置? [英] Techniques for sharing a config between two apps?

查看:88
本文介绍了技术的两个应用程序之间共享的配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序被分成一个配置工具,它写的配置和刚刚读取和使用设置从配置观众。

My app is split up into a configuration tool which writes the configuration and a viewer which just reads and uses settings from the configuration.

什么技术来存储性能将会在这种情况下建议?将XML作为不同类别是一个好主意?

What techniques for storing the properties would would be recommended in this scenario? Would XML for the different categories be a good idea?

该应用程序正在开发在C#中,在.NET 3.5和使用的WinForms。

The apps are developing in C#, on .NET 3.5 and using WinForms.

推荐答案

我将有一个共享程序集,其中包含您的设置类。然后,您可以序列化/反序列化这个类来一个共同的地方在硬盘上:

I would have a shared assembly, which contains your settings class. You can then serialize/deserialize this class to a common place on the hard drive:

[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文件将是这样的:

Your xml file will then look like this:

<?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>

这篇关于技术的两个应用程序之间共享的配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆