[Winform]应用程序配置如何保存?(ini or xml) 做的程序,有个设置面板,选项内容要保存下来,以便下次程序启动调用。。是用ini还是xml?或者其他方法??? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 都可以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; } }} 或是很简单的方法,也很利于扩展。用注册表Application.CommonAppDataRegistry.GetValue例子:string status = (Application.CommonAppDataRegistry.GetValue("StatusBar", bool.TrueString) as string) == bool.TrueString; ini文件的读写,需要引用一个api我从来不用,所以只能给你连接。http://www.cnblogs.com/zzyyll2/archive/2007/11/06/950584.html 在我目前的项目中,信息存放在.bin文件中,不过也是利用了序列化和反序列化的方式,跟萧遥兄的差不多 看来方法很多啊,个人觉得app.config很不错啊! 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(); } } 你这个面板的参数写到一个参数类里,给参数类加上特性[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); 看来大家都说config好,下次我也试试. app.config 也是xml吗???我新建了个。<?xml version="1.0" encoding="utf-8" ?><configuration></configuration> 用个单件 序列化到xml文件里面改了之后 Save一下就可以了 两者都可以用!不过我用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); } ASP.NET 图片浏览 释放 内存 c#类型转换 C# 大家帮帮看这句SQL错在那里了 winform中如何得到panel中的控件的Text 各位大侠,快帮忙!!!急!!! 调试出错啊 错误是::::未讲对象引用到实力 我怎样可以看到论坛300条以后的帖子? WebService ,Remoting ,Socket全接触 各位达人,在点击“关闭”按钮后,如何阻止关闭操作! windowsForms程序生成时提示错误 多线程下对一个集合添加和读取的问题
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;
}
}
}
Application.CommonAppDataRegistry.GetValue
例子:
string status = (Application.CommonAppDataRegistry.GetValue("StatusBar", bool.TrueString) as string) == bool.TrueString;
我从来不用,所以只能给你连接。
http://www.cnblogs.com/zzyyll2/archive/2007/11/06/950584.html
{
//文件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(); } }
保存: 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);
<configuration>
</configuration>
{
//读取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);
}