【转】ini载入保存类,操作INI配置文件方便的很

/******************************************************************
*
* ^_^ 恶猫 独门商标 挖哈哈
*
* QQ:\>23559055
*
* Site:\>http://emao.me
*
* ================================================================
*
* 说明:
*
* 1.命名空间: Emao_CfgManager
* 类名 : CfgManager
*
* 2.声明: CfgManager cm = new CfgManager();
*
* 3.具体方法自己看代码吧哈。简单的很。
*
* 分别是:add() readAll() writeAll()
*
******************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Runtime.InteropServices; namespace Emao_CfgManager
{
public class CfgManager
{
private Dictionary<string ,string> dict= new Dictionary<string,string>();
private string filename = Application.StartupPath + @"\config.ini"; #region API
[DllImport("kernel32.dll")]
public extern static int GetPrivateProfileString(string segName, string keyName, string sDefault, byte[] bt, int nSize, string fileName);
[DllImport("kernel32.dll")]
public extern static int WritePrivateProfileString(string segName, string keyName, string sValue, string fileName); [DllImport("kernel32.dll")]
public extern static int GetPrivateProfileSection(string segName, StringBuilder buffer, int nSize, string fileName);
[DllImport("kernel32.dll")]
public extern static int WritePrivateProfileSection(string segName, string sValue, string fileName); #endregion #region constructure
public CfgManager()
{
ReadAll();
}
public CfgManager(string inipath)
{
filename = inipath;
ReadAll();
}
#endregion #region method
public int Add(object key, object value)
{
if (this.dict.ContainsKey(key.ToString()))
this.dict[key.ToString()] = value.ToString();
else
this.dict.Add(key.ToString(), value.ToString());
return this.dict.Count;
}
public void ReadAll()
{
this.dict.Clear();
//读取ini文件所有
StringBuilder sb = new StringBuilder();
Byte[] bt = new Byte[];
int len = GetPrivateProfileString("Config", null, "", bt, bt.GetUpperBound(), filename);
if (len != )
{
int start = ;
for (int i = ; i < len; i++)
{
if ((bt[i] == ) && ((i - start) > ))
{
String s = Encoding.GetEncoding().GetString(bt, start, i - start);
Byte[] temp = new Byte[];
int i2 = GetPrivateProfileString("Config", s, "", temp, , filename);
string value = System.Text.Encoding.Default.GetString(temp).Replace('\0', ' ');
this.dict.Add(s, value.TrimEnd());
start = i + ;
}
}
}
}
public void ReadAll(string sectionName)
{
this.dict.Clear();
//读取指定节点
}
public void WriteAll()
{
foreach (var dic in dict)
{
WritePrivateProfileString("Config",dic.Key,dic.Value,filename);
}
}
public string this[string key]
{
get
{
if (key.Trim() == "")
{
return "";
}
if (!this.dict.ContainsKey(key)) return "";
return this.dict[key];
}
set
{
if (!this.dict.ContainsKey(key))
this.dict.Add(key, value);
else
this.dict[key] = value;
}
}
#endregion #region Function #endregion #region property
public int Count
{
get { return this.dict.Count; }
}
#endregion }
}

原文:http://www.oschina.net/code/snippet_191468_13277

上一篇:ASP.NET后台调用前台JS函数的三种常见方法


下一篇:BZOJ2151: 种树