本文介绍了如何使用读/写本地XML设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现类似的东西是什么,我需要在这里: HTTP://www.$c$cproject.com/KB/cs/PropertiesSettings的.aspx 但它并没有完全做到这一点对我来说。用户设置存储在一些遥远的位置,如 C:\ Documents和Settings \ [用户​​名] \本地设置\应用数据\ [应用程序] ,但我不能访问这些文件夹,我不能从一台计算机的设置复制文件到另一个,或者完全删除该文件。此外,这将是超级方便的旁边的应用程序设置的XML文件,并复制/船两者。这是用于演示洁具(这是一个合法的类型的编码的任务),将被用于由非技术人员在现场。我需要快速做出这一点,所以我需要重用一些现有的库,而不是写我自己。我需要使它易于使用和可移植的。我想的最后一件事是得到一个电话在午夜,指出设置时通过,我将已经建立的设置对话框编辑不坚持。

I found something similar to what I need here:http://www.codeproject.com/KB/cs/PropertiesSettings.aspxBut 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.

因此​​,用户设置存储上帝知道在哪里,和应用程序设置为只读(不走)。还有什么我可以做什么?我认为app.config文件,可谓一举多得,我想我曾经看到它被使用的方式我想,我只是找不到链接。

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.

推荐答案

您可以创建一个包含您的设置,然后一类XML序列化的:

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

这篇关于如何使用读/写本地XML设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:35