C#实现的一个内存Ini类
2011-05-24 09:51
204 查看
正式用上C#了,写了一个多星期代码了,感觉上来说,总体还蛮顺手的,直接拿来就写了。只是写的过程中,总是想着对象释放,这个比较蛋疼,我看了一些网上的代码貌似都是有new了,但是后面都没有释放,俺们还是写Delphi之类的习惯了,对象创建一写上,马上要在对应的位置写一个释放。貌似C#不必,但是总不放心,虽然说有垃圾回收机制,但是总怕有个闪失神马的。。。。。这个方面还得多找找相关资料看看具体的工作原理。
C#的很多特性,在写代码的时候是比较爽的,但是,也有时候比较蛋疼,我这写了几天,发现的几个比较蛋疼的就是调用Windows API还有就是以往在Delphi中用习惯的操作类库没得。不晓得,为啥微软不把他的那个NativeMethod的API库开放出来,如果开放了,直接用那个玩意不就可以直接用API咯,现在要用一下,每次都得DLLImport或者自己来封装,蛋疼啊!也可能是我不知道有这样的库吧!-_-!然后就是貌似没见到Ini操作的类库,莫非微软已经不用这个了,虽然说.net自己带有一个配置类可以直接操作,但是有时候这个还是需要的,网上搜索了一下,貌似都是自己封装的类库。这个也比较蛋疼。既然这个没有,那么像Delphi一样的TMemIniFile这个内存Ini操作类库估计就更蛋疼了。实际上这个是非常有必要的,因为很多时候,数据库中可能会存放这样的结构,这样就不会存在一个实际的Ini文件,那么WinAPI就起不了啥作用了,内存Ini解析就显得相当有必要。网上找了一番,确实也没发现内存操作的Ini类库。于是就自己实现了一个内存操作Ini的一个类库,现学现卖,开放给需要的人了,实际上代码并不难-_-!另外,初写C#或许难免有很多位置写的不太规范,希望大家有能给出中肯的指点!
用法很简单
读取
本内存Ini提供了LoadFromStream和SaveToStream,可以直接从内存加载,这样就可以很容易和数据库等字段结构交互了!
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类
- [C#]C#实现的一个先进先出的环形内存流
- C#实现的内存分页机制的一个实例
- C#实现的内存分页机制的一个实例
- C#实现的内存分页机制的一个实例
- Go/Python/Erlang编程语言对比分析及示例 基于RabbitMQ.Client组件实现RabbitMQ可复用的 ConnectionPool(连接池) 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!
- 一个用C#实现的简单http server(转)
- 【C#网络编程系列】专题十二:实现一个简单的FTP服务器
- 有两个升序排列的数组A1和A2,给A1开辟的剩余内存有足够空间容纳A1,请实现一个函数,把A2中所有数字插入到A1中,并且是按照升序排列的
- C#实现一个简单的多线程IP或端口扫描器
- 请实现一个类,时时刻刻记录内存中类的对象的个数
- C# 实现将一个文本文档按行数分成多个文档
- 判断一个字符串是否全是数字的多种方法及其性能比较(C#实现)--来源CSDN
- C#一个singleton的通用实现
- C# 实现一个窗体调用另一个窗体的控件
- 一个使用C#语言实现的数据比较案例
- c# 实现2个程序同时操作读写一个文件、一个读一个写
- 使用Unity3D的设计思想实现一个简单的C#赛车游戏场景
- C#实现一个最简单的HTTP服务器
- C# 实现的一个二叉树类