如何使用读/写本地XML设置?

发布于 2024-11-16 04:31:07 字数 625 浏览 3 评论 0原文

我在这里找到了与我需要的类似的东西: http://www.codeproject.com/KB/cs/PropertiesSettings.aspx 但它并不完全适合我。用户设置存储在一些较远的位置,例如 C:\documents and settings\[用户名]\local settings\application data\[your application],但我无权访问这些文件夹我无法将设置文件从一台计算机复制到另一台计算机,也无法完全删除该文件。此外,将设置 xml 文件放在应用程序旁边并复制/发送这两个文件会非常方便。这用于演示软件(这是一种合法的编码任务类型),并将由该领域的非技术人员使用。我需要快速完成此任务,因此我需要重用一些现有的库,而不是编写自己的库。我需要让它易于使用且便于携带。我最不想看到的就是在午夜接到电话,说通过我将构建的设置对话框进行编辑时,设置不会保留。

因此,用户设置存储在天知道在哪里,而应用程序设置是只读的(无法访问)。我还有什么可以做的吗?我认为 app.config 文件有多种用途,我想我曾经看到它按照我想要的方式使用,但我只是找不到链接。

如果有不清楚的地方请告诉我。

I found something similar to what I need here:
http://www.codeproject.com/KB/cs/PropertiesSettings.aspx
But it does not quite do it for me. The user settings are stored in some far away location such as C:\documents and settings\[username]\local settings\application data\[your application], but I do not have access to these folders and I cannot copy the settings file from one computer to another, or to delete the file altogether. Also, it would be super-convenient to have the settings xml file right next to the app, and to copy/ship both. This is used for demo-ware (which is a legitimate type of coding task) and will be used by non-technical people in the field. I need to make this quickly, so I need to reuse some existing library and not write my own. I need to make it easy to use and be portable. The last thing I want is to get a call at midnight that says that settings do not persist when edited through the settings dialog that I will have built.

So, user settings are stored god knows where, and application settings are read-only (no go). Is there anything else that I can do? I think app.config file has multiple purposes and I think I once saw it being used the way I want, I just cannot find the link.

Let me know if something is not clear.

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

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

发布评论

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

评论(1

戈亓 2024-11-23 04:31:07

您可以创建一个类来保存您的设置,然后对其进行 XML 序列化:

public class Settings
{
    public string Setting1 { get; set; }
    public int Setting2 { get; set; }
}

static void SaveSettings(Settings settings)
{
    var serializer = new XmlSerializer(typeof(Settings));
    using (var stream = File.OpenWrite(SettingsFilePath))
    {
        serializer.Serialize(stream, settings);
    }
}

static Settings LoadSettings()
{
    if (!File.Exists(SettingsFilePath))
        return new Settings();

    var serializer = new XmlSerializer(typeof(Settings));
    using (var stream = File.OpenRead(SettingsFilePath))
    {
        return (Settings)serializer.Deserialize(stream);
    }
}

You could create a class that holds your settings and then XML-serialize it:

public class Settings
{
    public string Setting1 { get; set; }
    public int Setting2 { get; set; }
}

static void SaveSettings(Settings settings)
{
    var serializer = new XmlSerializer(typeof(Settings));
    using (var stream = File.OpenWrite(SettingsFilePath))
    {
        serializer.Serialize(stream, settings);
    }
}

static Settings LoadSettings()
{
    if (!File.Exists(SettingsFilePath))
        return new Settings();

    var serializer = new XmlSerializer(typeof(Settings));
    using (var stream = File.OpenRead(SettingsFilePath))
    {
        return (Settings)serializer.Deserialize(stream);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文