ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 在dotnet中操作INI文件(转)

在dotnet中操作INI文件(转)

原创 Linux操作系统 作者:jcszjswkzhou 时间:2019-05-23 15:12:06 0 删除 编辑

using System;
using System.IO;
using System.Collections;

namespace Sinoprise.Configuration
{

public class IniFile
{

#region 外部导入函数


using System;
using System.IO;
using System.Collections;

namespace Sinoprise.Configuration
{

public class IniFile
{

#region 外部导入函数

//对ini文件进行写操作的函数
//[DllImport("kernel32",CharSet = CharSet.Auto)]
//protected static extern long WritePrivateProfileString(string section,string key,string val,string filePath);

//对ini文件进行读操作的函数
//[DllImport("kernel32",CharSet = CharSet.Auto)]
//protected static extern int GetPrivateProfileString(string section,string key,string def, StringBuilder retVal,int size,string filePath);

#endregion

private string m_fileName; //用于存放ini文件的路径和名称
private Hashtable m_sections; //用于存放整个ini文件的内容

public string FileName
{
get
{
return m_fileName;
}
}

///


/// IniFile的构造函数
///

/// ini文件的路径和名称
public IniFile(string fileName)
{
m_fileName = fileName;
m_sections = new Hashtable();
LoadValues();
}

~IniFile()
{
m_sections = null;
}


///


/// 装载Ini文件的内容
///

private void LoadValues()
{
if ((m_fileName.Trim()!="") && (File.Exists(m_fileName)))
{

using (StreamReader sr = new StreamReader(m_fileName))
{
SetStrings(sr);
}

}
}


///


/// 添加一个ini文件的Section
///

/// section的名字
/// 添加的section
private Hashtable AddSection(string item)
{
Hashtable sectionsItem = new Hashtable();
m_sections.Add(item,sectionsItem);
return sectionsItem;
}

///


/// 将ini文件的内容读取到内存当中
///

/// 读取文件的流
private void SetStrings(StreamReader sr)
{
String line;
Hashtable sectionsKeys = null;
int splitPos;
string keyName,keyValue;
while ((line = sr.ReadLine()) != null)
{
line = line.Trim();

if (line == "") continue;

if (line.Substring(0,1)!=";")
{
if ((line.Substring(0,1)=="[") && (line.Substring(line.Length-1,1)=="]"))
{
line = line.Substring(1,line.Length -2);
sectionsKeys = AddSection(line.Trim());
}

else
{

splitPos = line.IndexOf(´=´);
if ((splitPos>0) && (sectionsKeys != null))
{
keyName =line.Substring(0,splitPos).Trim();

if (keyName.Length == 0)
{
throw new Exception("IniFile Syntax Error!");
}

if (keyName.Length < line.Length - 1)
{
keyValue =line.Substring(splitPos+1,line.Length-2-splitPos) ;
}
else
{
keyValue = "";
}
sectionsKeys.Add(keyName,keyValue);
}

}
}

}
}


///


/// 将内容写回到Ini文件当中
///

public void UpdateFile()
{
Hashtable sectionsItem;

if (File.Exists(m_fileName))
{
File.Delete(m_fileName);
}

using (StreamWriter sw = new StreamWriter(m_fileName))
{

foreach(DictionaryEntry scnItem in m_sections)
{
sw.WriteLine("["+(string)scnItem.Key+"]");

sectionsItem=(Hashtable)m_sections[(string)scnItem.Key];
foreach(DictionaryEntry keyItem in sectionsItem)
{
sw.WriteLine((string)keyItem.Key+"="+(string)keyItem.Value);
}

sw.WriteLine("");
}

sw.Close();
}

}


///


/// 读取Ini文件所有的section
///

/// 以ArrayList形式返回以字符串表示的section
public ArrayList ReadSections()
{
ArrayList sectionList = new ArrayList();

foreach(DictionaryEntry item in m_sections)
{
sectionList.Add((string)item.Key);
}
return sectionList;
}


///


/// 读取Ini文件的一个section中的所有key
///

/// 要读取的section
/// 以ArrayList形式返回以字符串表示的kry
public ArrayList ReadKeys(string sectionName)
{
ArrayList keyList = new ArrayList();

Hashtable sectionsItem =(Hashtable)m_sections[sectionName.Trim()];

foreach(DictionaryEntry item in sectionsItem)
{
keyList.Add((string)item.Key);
}

return keyList;
}


///


/// 判断一个section是否存在
///

/// 要检查的section的名字
/// 如果section存在,则返回true,否则返回false
public bool SectionExists(string sectionName)
{
return m_sections.ContainsKey(sectionName.Trim());
}

///


/// 判断一个key在一个section中是否存在
///

/// 所指定的section
/// 所要检查的key的名字
/// 如果此key在这个section中存在,则返回true,否则返回false
public bool KeyExists(string sectionName,string keyName)
{
sectionName = sectionName.Trim();

if (!m_sections.ContainsKey(sectionName))
{
return false;
}

return ((Hashtable)m_sections[sectionName]).ContainsKey(keyName.Trim());
}

///


/// 删除一个section
///

/// 所要删除的section的名字
public void EraseSection(string sectionName)
{
m_sections.Remove(sectionName);
}


///
///在一个sectionk中删除一个key
///

/// 所指定的section
/// 所要删除的key的名字
public void DeleteKey(string sectionName,string keyName )
{
sectionName = sectionName.Trim();

if (!m_sections.ContainsKey(sectionName))
{
return;
}

((Hashtable)m_sections[sectionName]).Remove(keyName.Trim());
}


public string ReadString(string sectionName,string keyName,string defaultValue)
{
sectionName = sectionName.Trim();
keyName = keyName.Trim();

if (!m_sections.ContainsKey(sectionName))
{
return defaultValue;
}

Hashtable sectionsItem = (Hashtable)m_sections[sectionName];
if (!sectionsItem.ContainsKey(keyName))
{
return defaultValue;
}

return (string)sectionsItem[keyName];

}

public void WriteString(string sectionName,string keyName,string stringValue)
{
Hashtable sectionsItem;

sectionName = sectionName.Trim();
keyName = keyName.Trim();
stringValue = stringValue.Trim();


if (!m_sections.ContainsKey(sectionName))
{
sectionsItem = AddSection(sectionName);
}
else
{
sectionsItem = (Hashtable)m_sections[sectionName];
}


if (!sectionsItem.ContainsKey(keyName))
{
sectionsItem[keyName] = stringValue;
}
else
{
sectionsItem.Add(keyName,stringValue);
}
}



public long ReadInteger(string sectionName,string keyName,long defaultValue)
{
return Convert.ToInt64(ReadString(sectionName,keyName,Convert.ToString(defaultValue)));
}

public void WriteInteger(string sectionName,string keyName,long longValue)
{
WriteString(sectionName,keyName,Convert.ToString(longValue));
}



public bool ReadBool(string sectionName,string keyName,bool defaultValue)
{
return Convert.ToBoolean(ReadString(sectionName,keyName,Convert.ToString(defaultValue)));
}

public void WriteBool(string sectionName,string keyName,bool boolValue)
{
WriteString(sectionName,keyName,Convert.ToString(boolValue));
}


public int ReadBinaryStream(string sectionName,string keyName,Stream defaultValue)
{
return 0;
}
public void WriteBinaryStream(string sectionName,string keyName,Stream streamValue)
{}



public DateTime ReadDate(string sectionName,string keyName,DateTime defaultValue)
{
return Convert.ToDateTime(ReadString(sectionName,keyName,Convert.ToString(defaultValue))).Date;
}
public void WriteDate(string sectionName,string keyName,DateTime dateValue)
{
WriteString(sectionName,keyName,Convert.ToString(dateValue.Date));
}



public DateTime ReadDateTime(string sectionName,string keyName,DateTime defaultValue)
{
return Convert.ToDateTime(ReadString(sectionName,keyName,Convert.ToString(defaultValue)));
}
public void WriteDateTime(string sectionName,string keyName,DateTime dateTimeValue)
{
WriteString(sectionName,keyName,Convert.ToString(dateTimeValue));
}


public double ReadFloat(string sectionName,string keyName,double defaultValue)
{
return Convert.ToDouble(ReadString(sectionName,keyName,Convert.ToString(defaultValue)));
}
public void WriteFloat(string sectionName,string keyName,double doubleValue)
{
WriteString(sectionName,keyName,Convert.ToString(doubleValue));
}


public DateTime ReadTime(string sectionName,string keyName,DateTime defaultValue)
{
return Convert.ToDateTime(ReadString(sectionName,keyName,Convert.ToString(defaultValue)));
}
public void WriteTime(string sectionName,string keyName,DateTime timeValue)
{
WriteString(sectionName,keyName,Convert.ToString(timeValue.TimeOfDay));
}


}
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10294527/viewspace-122580/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-08-29

  • 博文量
    3488
  • 访问量
    2621278