C#实现的一个内存Ini类
? ? 正式用上C#了,寫了一個多星期代碼了,感覺上來說,總體還蠻順手的,直接拿來就寫了。只是寫的過程中,總是想著對象釋放,這個比較蛋疼,我看了一些網上的代碼貌似都是有new了,但是后面都沒有釋放,俺們還是寫Delphi之類的習慣了,對象創建一寫上,馬上要在對應的位置寫一個釋放。貌似C#不必,但是總不放心,雖然說有垃圾回收機制,但是總怕有個閃失神馬的。。。。。這個方面還得多找找相關資料看看具體的工作原理。
? ? C#的很多特性,在寫代碼的時候是比較爽的,但是,也有時候比較蛋疼,我這寫了幾天,發現的幾個比較蛋疼的就是調用Windows API還有就是以往在Delphi中用習慣的操作類庫沒得。不曉得,為啥微軟不把他的那個NativeMethod的API庫開放出來,如果開放了,直接用那個玩意不就可以直接用API咯,現在要用一下,每次都得DLLImport或者自己來封裝,蛋疼啊!也可能是我不知道有這樣的庫吧!-_-!然后就是貌似沒見到Ini操作的類庫,莫非微軟已經不用這個了,雖然說.net自己帶有一個配置類可以直接操作,但是有時候這個還是需要的,網上搜索了一下,貌似都是自己封裝的類庫。這個也比較蛋疼。既然這個沒有,那么像Delphi一樣的TMemIniFile這個內存Ini操作類庫估計就更蛋疼了。實際上這個是非常有必要的,因為很多時候,數據庫中可能會存放這樣的結構,這樣就不會存在一個實際的Ini文件,那么WinAPI就起不了啥作用了,內存Ini解析就顯得相當有必要。網上找了一番,確實也沒發現內存操作的Ini類庫。于是就自己實現了一個內存操作Ini的一個類庫,現學現賣,開放給需要的人了,實際上代碼并不難-_-!另外,初寫C#或許難免有很多位置寫的不太規范,希望大家有能給出中肯的指點!
/// <summary>/// Ini節點
/// </summary>
public class IniSection
{
private Dictionary<string, string> FDictionary;//節點值
private String FSectionName;//節點名稱
public IniSection(String SName)
{
FSectionName = SName;
FDictionary = new Dictionary<string, string>();
}
public string SectionName
{
get { return FSectionName; }
}
public int Count
{
get { return FDictionary.Count; }
}
public void Clear()
{
FDictionary.Clear();
}
//增加鍵值對
public void AddKeyValue(string key, string value)
{
if (FDictionary.ContainsKey(key))
FDictionary[key] = value;
else
FDictionary.Add(key, value);
}
public void WriteValue(string key, string value)
{
AddKeyValue(key, value);
}
public void WriteValue(string key, bool value)
{
AddKeyValue(key,Convert.ToString(value));
}
public void WriteValue(string key, int value)
{
AddKeyValue(key, Convert.ToString(value));
}
public void WriteValue(string key, float value)
{
AddKeyValue(key, Convert.ToString(value));
}
public void WriteValue(string key, DateTime value)
{
AddKeyValue(key, Convert.ToString(value));
}
public string ReadValue(string key,string defaultv)
{
if (FDictionary.ContainsKey(key))
return FDictionary[key];
else
return defaultv;
}
public bool ReadValue(string key, bool defaultv)
{
string rt = ReadValue(key, Convert.ToString(defaultv));
return Convert.ToBoolean(rt);
}
public int ReadValue(string key, int defaultv)
{
string rt = ReadValue(key, Convert.ToString(defaultv));
return Convert.ToInt32(rt);
}
public float ReadValue(string key, float defaultv)
{
string rt = ReadValue(key, Convert.ToString(defaultv));
return Convert.ToSingle(rt);
}
public DateTime ReadValue(string key, DateTime defaultv)
{
string rt = ReadValue(key, Convert.ToString(defaultv));
return Convert.ToDateTime(rt);
}
public void SaveToStream(Stream stream)
{
StreamWriter SW = new StreamWriter(stream);
SaveToStream(SW);
SW.Dispose();
}
public void SaveToStream(StreamWriter SW)
{
SW.WriteLine("[" + FSectionName + "]");
foreach (KeyValuePair<string, string> item in FDictionary)
{
SW.WriteLine(item.Key + "=" + item.Value);
}
}
}
/// <summary>
/// 內存Ini解析
/// </summary>
public class MemIniFile
{
private ArrayList List;//所有節點信息
private bool SectionExists(string SectionName)
{
foreach (IniSection ISec in List)
{
if (ISec.SectionName.ToLower() == SectionName.ToLower())
return true;
}
return false;
}
public IniSection FindSection(string SectionName)
{
foreach (IniSection ISec in List)
{
if (ISec.SectionName.ToLower() == SectionName.ToLower())
return ISec;
}
return null;
}
public MemIniFile()
{
List = new ArrayList();
}
public void LoadFromStream(Stream stream)
{
StreamReader SR = new StreamReader(stream);
List.Clear();
string st = null;
IniSection Section = null;//節點
int equalSignPos;
string key, value;
while (true)
{
st = SR.ReadLine();
if (st == null)
break;
st = st.Trim();
if (st == "")
continue;
if (st != "" && st[0] == '[' && st[st.Length - 1] == ']')
{
st = st.Remove(0,1);
st = st.Remove(st.Length - 1,1);
Section = FindSection(st);
if (Section == null)
{
Section = new IniSection(st);
List.Add(Section);
}
}
else
{
if (Section == null)
{
Section = FindSection("UnDefSection");
if (Section == null)
{
Section = new IniSection("UnDefSection");
List.Add(Section);
}
}
//開始解析
equalSignPos = st.IndexOf('=');
if (equalSignPos != 0)
{
key = st.Substring(0, equalSignPos);
value = st.Substring(equalSignPos + 1, st.Length - equalSignPos - 1);
Section.AddKeyValue(key, value);//增加到節點
}
else
Section.AddKeyValue(st, "");
}
}
SR.Dispose();
}
public void SaveToStream(Stream stream)
{
StreamWriter SW = new StreamWriter(stream);
foreach (IniSection ISec in List)
{
ISec.SaveToStream(SW);
}
SW.Dispose();
}
public string ReadValue(string SectionName, string key, string defaultv)
{
IniSection ISec = FindSection(SectionName);
if (ISec != null)
{
return ISec.ReadValue(key, defaultv);
}
else return defaultv;
}
public bool ReadValue(string SectionName, string key, bool defaultv)
{
IniSection ISec = FindSection(SectionName);
if (ISec != null)
{
return ISec.ReadValue(key, defaultv);
}
else return defaultv;
}
public int ReadValue(string SectionName, string key, int defaultv)
{
IniSection ISec = FindSection(SectionName);
if (ISec != null)
{
return ISec.ReadValue(key, defaultv);
}
else return defaultv;
}
public float ReadValue(string SectionName, string key, float defaultv)
{
IniSection ISec = FindSection(SectionName);
if (ISec != null)
{
return ISec.ReadValue(key, defaultv);
}
else return defaultv;
}
public DateTime ReadValue(string SectionName, string key, DateTime defaultv)
{
IniSection ISec = FindSection(SectionName);
if (ISec != null)
{
return ISec.ReadValue(key, defaultv);
}
else return defaultv;
}
public IniSection WriteValue(string SectionName, string key, string value)
{
IniSection ISec = FindSection(SectionName);
if (ISec == null)
{
ISec = new IniSection(SectionName);
List.Add(ISec);
}
ISec.WriteValue(key, value);
return ISec;
}
public IniSection WriteValue(string SectionName, string key, bool value)
{
IniSection ISec = FindSection(SectionName);
if (ISec == null)
{
ISec = new IniSection(SectionName);
List.Add(ISec);
}
ISec.WriteValue(key, value);
return ISec;
}
public IniSection WriteValue(string SectionName, string key, int value)
{
IniSection ISec = FindSection(SectionName);
if (ISec == null)
{
ISec = new IniSection(SectionName);
List.Add(ISec);
}
ISec.WriteValue(key, value);
return ISec;
}
public IniSection WriteValue(string SectionName, string key, float value)
{
IniSection ISec = FindSection(SectionName);
if (ISec == null)
{
ISec = new IniSection(SectionName);
List.Add(ISec);
}
ISec.WriteValue(key, value);
return ISec;
}
public IniSection WriteValue(string SectionName, string key, DateTime value)
{
IniSection ISec = FindSection(SectionName);
if (ISec == null)
{
ISec = new IniSection(SectionName);
List.Add(ISec);
}
ISec.WriteValue(key, value);
return ISec;
}
public void LoadFromFile(string FileName)
{
FileStream FS = new FileStream(System.IO.Path.GetFullPath(FileName), FileMode.Open);
LoadFromStream(FS);
FS.Close();
FS.Dispose();
}
public void SaveToFile(string FileName)
{
FileStream FS = new FileStream(System.IO.Path.GetFullPath(FileName), FileMode.Create);
SaveToStream(FS);
FS.Close();
FS.Dispose();
}
}
用法很簡單
MemIniFile MIni = new MemIniFile();IniSection ISec = MIni.WriteValue("系統配置", "Con", "鏈接測試");
ISec.WriteValue("pwd", "124");
ISec.WriteValue("Port", 345);
MIni.SaveToFile("1.ini");
讀取
MemIniFile Mini = new MemIniFile();Mini.LoadFromFile("1.txt");
Mini.ReadValue("系統配置", "pwd", ""); IniSection ISec = Mini.FindSection("系統配置");ISec.ReadValue("Port", 345); 本內存Ini提供了LoadFromStream和SaveToStream,可以直接從內存加載,這樣就可以很容易和數據庫等字段結構交互了!
總結
以上是生活随笔為你收集整理的C#实现的一个内存Ini类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java的自动装箱与拆箱
- 下一篇: ‘人’字的来历