代码之家  ›  专栏  ›  技术社区  ›  echoblaze

将配置文件设置为XML文件

  •  2
  • echoblaze  · 技术社区  · 17 年前

    3 回复  |  直到 17 年前
        1
  •  1
  •   Dave Arkell    17 年前

    您可以使用该系统。Xml。序列化。XmlSerializer类,用于自动将设置拉入自定义类。

    使用您的设置创建类:

    public class Settings
    {
        private string connectionString;
    
        public string ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; }
        }
    }
    

    Settings settings = new Settings();
    FileStream filestream = new FileStream("settings.xml", FileMode.Open);
    System.Xml.Serialization.XmlSerializer cereals = new System.Xml.Serialization.XmlSerializer(typeof(Settings));
    settings = cereals.Deserialize(filestream);
    

    XmlSerializer cereals = new XmlSerializer(typeof(Settings));
    System.IO.FileStream writer = new FileStream("settings.xml", FileMode.Create);
    cereals.Serialize(writer, settings);
    

    在这种情况下,“settings.xml”文件位于当前目录中,但我通常将其放在用户的应用程序数据文件夹中,因为您始终可以写入该文件夹。

        2
  •  0
  •   Community Mohan Dere    9 年前

    您可以使用app.config文件,也可以创建自己的XML文件来存储它们

    Simplest way to have a configuration file in a Windows Forms C# Application

        3
  •  0
  •   gatapia    16 年前

    试试我下面关于这个主题的帖子,它与戴夫上面提出的解决方案非常相似,但只是有点肉。 http://www.picnet.com.au/blogs/Guido/post/2009/09/10/XML-Settings-Files-No-more-webconfig.aspx