.net 在两个程序之间共享配置数据?

发布于 2024-12-20 13:48:28 字数 194 浏览 0 评论 0原文

我正在使用应用程序设置(作为 Visual Studio 的一部分),但似乎无法使用原始应用程序中的设置来获取其他应用程序。

有人可以帮助在 .net c# 中的两个应用程序之间存储一些字符串变量吗?

编辑:似乎我需要从第二个应用程序添加对我的第一个应用程序的引用才能访问 Properties.Default 设置 - 我该如何执行此操作?

I was using Application Settings (as part of Visual Studio) but can't seem to get the other app using the settings from the original app.

Can someone help with storing a few string variables between two apps in .net c#?

EDIT: seems I need to add a reference to my first app from the second app to access the Properties.Default settings - how do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恏ㄋ傷疤忘ㄋ疼 2024-12-27 13:48:28

如果您想在项目之间共享配置,您只需添加其他项目中的文件“作为链接”:右键单击项目>>选择“添加现有文件” >>导航到 app.config 文件>>单击添加按钮旁边的下拉菜单,然后选择添加为链接

如果您想在两个应用程序之间共享配置,这是

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

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

if you want to share a config between to projects you can just add the file from the other project 'as a link': Right click on the project >> select 'Add existing file' >> navigate to the app.config file >> click the dropdown next to the add button and select add as a link.

or

if you want to share a config between two apps this is the method

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();
        }
    }
}

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>
很快妥协 2024-12-27 13:48:28

如果您希望同一用户对两个应用程序使用相同的设置,或者希望所有用户共享相同的设置,除了标准应用程序设置之外,您还有以下几种选择:

1) 将数据存储在注册表中。您可以将设置设置为用户特定的或机器全局的。

2) 将包含设置的结构化文件(例如 XML 文件)存储在标准目录之一中:适用于所有用户的 Environment.SpecialFolder.CommonApplicationData 或适用于单个用户的 Environment.SpecialFolder.ApplicationData。这将是我将使用的方法。

If you want the same user to use the same settings for both apps OR you want all users to share the same settings, you have a couple of choices besides the standard appsettings:

1) Store the data in the registry. You could either make the settings user specific or global to the machine.

2) Store a structured file, such as an XML file, containing the settings in one of the standard directories: Environment.SpecialFolder.CommonApplicationData for all users or Environment.SpecialFolder.ApplicationData for a single user. This would be the approach I would use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文