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,可以直接从内存加载,这样就可以很容易和数据库等字段结构交互了!


本文转自 不得闲 博客园博客,原文链接:  http://www.cnblogs.com/DxSoft/archive/2011/05/24/2055050.html ,如需转载请自行联系原作者


相关文章
|
1月前
|
C#
C#学习相关系列之数据类型类的三大特性(二)
C#学习相关系列之数据类型类的三大特性(二)
|
1月前
|
C#
58.c#:directory类
58.c#:directory类
12 0
|
1月前
|
C#
57.c#:directorylnfo类
57.c#:directorylnfo类
13 0
|
1月前
|
监控 C#
55.c#:file类
55.c#:file类
16 1
|
1月前
|
算法 C#
54.c#:random类
54.c#:random类
14 1
|
1月前
|
C#
51.c#:string类的静态方法
51.c#:string类的静态方法
20 1
|
1月前
|
C#
27.c#关键字sealed修饰类
27.c#关键字sealed修饰类
12 0
|
3月前
|
Java C#
C# 面向对象编程解析:优势、类和对象、类成员详解
OOP代表面向对象编程。 过程式编程涉及编写执行数据操作的过程或方法,而面向对象编程涉及创建包含数据和方法的对象。 面向对象编程相对于过程式编程具有几个优势: OOP执行速度更快,更容易执行 OOP为程序提供了清晰的结构 OOP有助于保持C#代码DRY("不要重复自己"),并使代码更易于维护、修改和调试 OOP使得能够创建完全可重用的应用程序,编写更少的代码并减少开发时间 提示:"不要重复自己"(DRY)原则是有关减少代码重复的原则。应该提取出应用程序中常见的代码,并将其放置在单一位置并重复使用,而不是重复编写。
51 0
|
1月前
|
C#
深入C#中的String类
深入C#中的String类
11 0
|
1月前
|
C#
C#学习系列相关之多线程(二)----Thread类介绍
C#学习系列相关之多线程(二)----Thread类介绍