做的程序,有个设置面板,选项内容要保存下来,以便下次程序启动调用。。是用ini还是xml?或者其他方法???

解决方案 »

  1.   

    都可以
    xml的,我自己个人项目中的,贴给你用吧。任意类型到xml的序列化和反序列化。using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;
    using System.IO;namespace RegexActivator
    {
        public class ExtendMethods
        {
            public static void Serial<T>(T[] items, string path)
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T[]));
                TextWriter writer = new StreamWriter(path);
                try
                {
                    xmlSerializer.Serialize(writer, items);
                }
                finally
                {
                    writer.Close();
                }
            }        public static T[] Deserial<T>(string path)
            {
                if (!File.Exists(path)) return new T[0];
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T[]));
                FileStream fs = new FileStream(path, FileMode.Open);
                T[] items;
                try
                {
                    items = (T[])xmlSerializer.Deserialize(fs);
                }
                finally
                {
                    fs.Close();
                }
                return items;
            }
        }
    }
      

  2.   

    或是很简单的方法,也很利于扩展。用注册表
    Application.CommonAppDataRegistry.GetValue
    例子:
    string status = (Application.CommonAppDataRegistry.GetValue("StatusBar", bool.TrueString) as string) == bool.TrueString;
      

  3.   

    ini文件的读写,需要引用一个api
    我从来不用,所以只能给你连接。
    http://www.cnblogs.com/zzyyll2/archive/2007/11/06/950584.html
      

  4.   

    在我目前的项目中,信息存放在.bin文件中,不过也是利用了序列化和反序列化的方式,跟萧遥兄的差不多
      

  5.   

    看来方法很多啊,个人觉得app.config很不错啊!
      

  6.   

    ini也行啊public class IniFile
    {
    //文件INI名称 
    public   string   Path;  ////声明读写INI文件的API函数   
    [DllImport( "kernel32 ")]  private   static   extern   long   WritePrivateProfileString(string   section,string   key,string   val,string   filePath); 
    [DllImport( "kernel32 ")]  private   static   extern   int   GetPrivateProfileString(string   section,string   key,string   def,StringBuilder   retVal,int   size,string   filePath); 
    //类的构造函数,传递INI文件名 
    public   IniFile(string   inipath) 

    // 
    //   TODO:   Add   constructor   logic   here 
    // 
    Path   =   inipath; 
    }  //写INI文件 
    public   void   IniWriteValue(string   Section,string   Key,string   Value) 

    WritePrivateProfileString(Section,Key,Value,this.Path);  }  //读取INI文件指定 
    public   string   IniReadValue(string   Section,string   Key) 

    StringBuilder   temp   =   new   StringBuilder(255); 
    int   i   =   GetPrivateProfileString(Section,Key, " ",temp,255,this.Path); 
    return   temp.ToString();  }  }
      

  7.   

    你这个面板的参数写到一个参数类里,给参数类加上特性[Serializable]
    保存: FileStream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
    new BinaryFormatter().Serialize(stream, obj);获取:FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    new BinaryFormatter().Deserialize(stream);
      

  8.   

    看来大家都说config好,下次我也试试.
      

  9.   

    app.config 也是xml吗???我新建了个。<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    </configuration>
      

  10.   

    用个单件  序列化到xml文件里面改了之后 Save一下就可以了
      

  11.   

    两者都可以用!不过我用xml会更麻烦些!  class ReadIniFile
        {
            //读取INI文件的类构造
            [DllImport("kernel32")]
            private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
            [DllImport("kernel32")]
            private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
            //读取INI文件的相关参数
            public static string IniReadValue(string Section, string Key)
            {
                string syspath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                string FileName = syspath + "\\EntryClient.ini";
                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(Section, Key, "", temp, 255, FileName);
                return temp.ToString();        }
            // 写入INI文件格式
            static string syspath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取应用程序路径
            public static void WriteCarRegister(string section, string Key, string Value)
            {
                string s_Section = section;
                string FileName = syspath + "\\EntryClient.ini";
                WritePrivateProfileString(s_Section, Key, Value, FileName);
            }