正式用上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类相关推荐

  1. 我也分享一个c# ini操作类

    刚刚看了一篇 @云菲菲 的关于基于正则的INI辅助类文章:http://www.cnblogs.com/yunfeifei/p/4081977.html,作者写的不错.还看到评论处有一个的地址:htt ...

  2. 轻松实现一个操作ini文件的类

    作者:lixiaosan(CSDN) 前言: 相信很多朋友在编写自己的程序中,都需要把一些数据先期导入到程序中进行初始化.那么这个时候,比较好的做法就是把你所有的数据写入一个ini文件,然后在程序去读 ...

  3. python 元类工厂模式_Python进阶丨如何创建你的第一个Python元类?

    摘要:通过本文,将深入讨论Python元类,其属性,如何以及何时在Python中使用元类. Python元类设置类的行为和规则.元类有助于修改类的实例,并且相当复杂,是Python编程的高级功能之一. ...

  4. java如何创造一个整数的类_【技术干货】Java 面试宝典:Java 基础部分(1)

    原标题:[技术干货]Java 面试宝典:Java 基础部分(1) Java基础部分: 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语法,集合的语法,io 的 ...

  5. 如何创建你的第一个Python元类?

    Python元类设置类的行为和规则.元类有助于修改类的实例,并且相当复杂,是Python编程的高级功能之一.通过本文,将深入讨论Python元类,其属性,如何以及何时在Python中使用元类.本文介绍 ...

  6. 面试必问:用 Java 写一个内存泄漏程序

    编译:ImportNew/唐尤华 原文链接:stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java 问题: 刚参加的一 ...

  7. python火狐配置文件_Python+Selenium中级篇之4-封装一个自己的类-浏览器引擎类/Python读取配置文件内容...

    封装一个自己的类-浏览器引擎类 前一篇文章我们知道了,如何去封装几个简单的Selenium方法到我们自定义的类,这次我们编写一个类,叫浏览器引擎类,通过更改一个字符串的值,利用if语句去判断和控制启动 ...

  8. Python进阶丨如何创建你的第一个Python元类?

    摘要:通过本文,将深入讨论Python元类,其属性,如何以及何时在Python中使用元类. Python元类设置类的行为和规则.元类有助于修改类的实例,并且相当复杂,是Python编程的高级功能之一. ...

  9. 【Objective-C】05-第一个OC的类

    OC是一门面向对象的语言,因此它也有类.对象.静态\动态方法.成员变量的概念.这讲就来创建第一个OC的类. 一.语法简介 1.类 在Java中,我们用1个.java文件就可以描述清楚一个类:在OC中, ...

最新文章

  1. 利用XSLT把ADO记录集转换成XML
  2. leetcode算法题--多米诺与托米诺平铺★
  3. C#中如何将光标定位在某个控件中?
  4. 强化学习(四)---基于模型动态规划问题
  5. Redis.conf常见配置介绍
  6. Java Integer类详解
  7. Python一直报错:SyntaxError: invalid syntax 的原因及解决办法
  8. 分布式缓存技术memcached学习系列(五)—— memcached java客户端的使用
  9. GridView控件日期格式化
  10. CA虚拟环境访问控制为虚拟化撑起保护伞
  11. 轻量级自动化运维工具ansible之一:初步介绍及简单运用
  12. UML教程6:状态图
  13. java 主类 测试类_Java中的测试类和主类分别是什么,有点晕啊。?
  14. win10设置计算机关机时间,Win10怎么设置自动关机时间_Win10设置自动关机教程-192路由网...
  15. go语言读取xls表格xls文件操作替代解决方案
  16. 名帖107 俞和 小楷临《乐毅论》
  17. MyBatis关联对象查询
  18. 计算机专业实习经验总结
  19. 生产服务器的pcie错误
  20. python手机壁纸高清_python爬取手机壁纸

热门文章

  1. 【Django】用pycharm初学习使用Django
  2. 【bzoj2154】Crash的数字表格 莫比乌斯反演
  3. 编辑距离及最小编辑距离算法(转)----动态规划
  4. [转发]SPRING MVC3.2案例讲解--SPRING MVC3的@ResponseBody和ResponseEntity
  5. js中使用进行字符串传参
  6. codeforces315Div1 B Symmetric and Transitive
  7. 线上分享 | AI产品经理之路——从入门到进阶
  8. 思维、视角、设计丨产品经理的三板斧
  9. 后台产品经理跳坑“指南”
  10. 【原创译文】Jive Circle案例学习:以用户为中心的设计