📄 iniconfig.cs
字号:
using System;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
namespace cn.com.daway
{
/// <summary>
/// ini文件键值
/// </summary>
struct iniKeyInfo
{
public string m_strKey; //键
public string m_strValue; //值
public string m_strMemo; //注释
}
/// <summary>
/// ini文件节
/// </summary>
struct iniSectionInfo
{
public string m_strSection; //节
public ArrayList m_Keys; //键值
public string m_strMemo; //注释
}
/// <summary>
/// ini文件
/// </summary>
public class IniConfig
{
private bool m_bExist; //文件是否存在
private string m_strFile; //文件路径
private ArrayList m_iniFile=null; //文件内容
private const string ERROR_FORMAT="错误的文件格式";
private const string ERROR_FILE_EXIST="文件不存在";
private const string ERROR_FILE_NULL="文件为空";
private const string ERROR_NOTFOUND_SECTION="没有找到相匹配的节";
private const string ERROR_NOTFOUND_KEY="没有找到相匹配的键";
/// <summary>
/// ini析构函数,释放m_iniFile
/// </summary>
~IniConfig()
{
if(m_iniFile!=null)
m_iniFile.Clear();
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
if(m_iniFile!=null)
m_iniFile.Clear();
GC.SuppressFinalize(this);
}
/// <summary>
/// 得到当前应用程序路径
/// </summary>
public static string GetAppPath()
{
return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}
/// <summary>
/// ini文件构造函数
/// </summary>
/// <param name="strFile">文件路径,以/开头表示绝对路径,否则为相对路径</param>
public IniConfig(string strFile)
{
string strAppPath=GetAppPath();
if(strFile.Substring(0,1)!="\\")
{
if(strAppPath=="\\")
m_strFile="\\"+strFile;
else
m_strFile=strAppPath+"\\"+strFile;
}
else
m_strFile=strFile;
System.IO.FileInfo fi=new FileInfo(m_strFile);
if(!fi.Exists)
m_bExist=false;
else
m_bExist=true;
try
{
if(m_bExist)
Prase();
}
catch(Exception err)
{
throw err;
}
}
/// <summary>
/// ini文件构造函数,带默认值,默认为应用程序目录下config.ini
/// </summary>
public IniConfig()
{
string strAppPath=GetAppPath();
if(strAppPath=="\\")
m_strFile="\\config.ini";
else
m_strFile=strAppPath+"\\config.ini";
System.IO.FileInfo fi=new FileInfo(m_strFile);
if(!fi.Exists)
m_bExist=false;
else
m_bExist=true;
try
{
if(m_bExist)
Prase();
}
catch(Exception err)
{
throw err;
}
}
/// <summary>
/// 文件是否存在
/// </summary>
public bool isExist()
{
return m_bExist;
}
/// <summary>
/// 分析ini文件,格式错误就抛出错误
/// </summary>
private void Prase()
{
m_iniFile=new ArrayList();
FileInfo fi=new FileInfo(m_strFile);
TextReader tr=fi.OpenText();
string strLine,strMemo=null;
strLine=tr.ReadLine();
iniKeyInfo key;
iniSectionInfo section;
iniSectionInfo tmpSection;
int nLine=0;
int nSection,nMemo,nKey;
bool bFlag=false;
string strErrorMsg;
Exception err;
do
{
if(strLine==null)
return;
strLine=strLine.Trim();
if(strLine=="")
continue;
nLine++;
if(strLine.Substring(0,1)=="[") //开始小节
{
//提取注释
nMemo=strLine.IndexOf(";");
if(nMemo<strLine.Length-1 && nMemo!=-1) //有注释
{
section.m_strMemo=strLine.Substring(nMemo,strLine.Length-1).Trim();
}
nSection=strLine.IndexOf("]");
if(nSection<2) //有'['无']'或[]无内容
{
strErrorMsg=string.Format("{0}\r\n{1}行\r\n{2}",ERROR_FORMAT,nLine,strLine);
err=new Exception(strErrorMsg);
throw err;
}
section=new iniSectionInfo();
section.m_strSection=strLine.Substring(1,nSection-1).Trim();
if(section.m_strSection.IndexOf(";")!=-1) //[]里有;
{
strErrorMsg=string.Format("{0}\r\n{1}行\r\n{2}",ERROR_FORMAT,nLine,strLine);
err=new Exception(strErrorMsg);
throw err;
}
bFlag=true;
section.m_Keys=new ArrayList();
m_iniFile.Add(section);
}
else
{
key=new iniKeyInfo();
string strNewLine;
nMemo=strLine.IndexOf(";");
if(nMemo<strLine.Length-1 && nMemo!=-1) //有注释
{
strMemo=strLine.Substring(nMemo,strLine.Length-nMemo).Trim();
}
if(nMemo!=-1)
strNewLine=strLine.Substring(0,nMemo).Trim();
else
strNewLine=strLine;
nKey=strNewLine.IndexOf("=");
if(nKey>=0 || (nMemo!=-1 && nKey>=nMemo))
{
key.m_strKey=strNewLine.Substring(0,nKey).Trim();
key.m_strValue=strNewLine.Substring(nKey+1,strNewLine.Length-nKey-1).Trim();
}
key.m_strMemo=strMemo;
if(!bFlag && key.m_strKey==null)
{
//只有解释,忽略
}
else
{
if(!bFlag)
{
strErrorMsg=string.Format("{0}\r\n{1}行\r\n{2}",ERROR_FORMAT,nLine,strLine);
err=new Exception(strErrorMsg);
throw err;
}
else
{
tmpSection=(iniSectionInfo)m_iniFile[m_iniFile.Count-1];
m_iniFile.RemoveAt(m_iniFile.Count-1);
tmpSection.m_Keys.Add(key);
m_iniFile.Add(tmpSection);
}
}
}
}
while((strLine=tr.ReadLine())!=null);
tr.Close();
}
/// <summary>
/// 取得某一节,某一键值
/// </summary>
/// <param name="strSection">某一节</param>
/// <param name="strKey">某一键</param>
/// <returns>如果有返回键值,否则抛出错误</returns>
public string GetProfileString(string strSection,string strKey)
{
string strValue=null;
string strErrorMsg;
Exception err;
if(!m_bExist)
{
strErrorMsg=string.Format("文件{0}{1}",m_strFile,ERROR_FILE_EXIST);
err=new Exception(strErrorMsg);
throw err;
}
else
{
if(m_iniFile==null)
{
strErrorMsg=string.Format("{0}\r\n文件{1}",ERROR_FILE_NULL,m_strFile);
err=new Exception(strErrorMsg);
throw err;
}
else
{
foreach(iniSectionInfo section in m_iniFile)
{
if(section.m_strSection==strSection)
{
foreach(iniKeyInfo key in section.m_Keys)
{
if(key.m_strKey!=null && key.m_strKey==strKey)
{
strValue=key.m_strValue;
return strValue;
}
}
strErrorMsg=string.Format("{0}\r\n键{1}",ERROR_NOTFOUND_KEY,strKey);
err=new Exception(strErrorMsg);
throw err;
}
}
strErrorMsg=string.Format("{0}\r\n节{1}",ERROR_NOTFOUND_SECTION,strSection);
err=new Exception(strErrorMsg);
throw err;
}
}
}
/// <summary>
/// 写某一节,某一键值
/// </summary>
/// <param name="strSection">某一节</param>
/// <param name="strKey">某一键</param>
/// <param name="strValue">键值</param>
/// <returns>如果成功无返回值,否则抛出错误</returns>
public void WriteProfileString(string strSection,string strKey,string strValue)
{
try
{
foreach(iniSectionInfo section in m_iniFile)
{
if(section.m_strSection==strSection)
{
foreach(iniKeyInfo key in section.m_Keys)
{
if(key.m_strKey==strKey)
{
section.m_Keys.Remove(key);
iniKeyInfo newKey=new iniKeyInfo();
newKey.m_strKey=strKey;
newKey.m_strValue=strValue;
section.m_Keys.Add(newKey);
WriteFile();
return ;
}
}
iniKeyInfo newKey1=new iniKeyInfo();
newKey1.m_strKey=strKey;
newKey1.m_strValue=strValue;
section.m_Keys.Add(newKey1);
WriteFile();
return;
}
}
iniSectionInfo newSection=new iniSectionInfo();
newSection.m_strSection=strSection;
newSection.m_Keys=new ArrayList();
iniKeyInfo newKey2=new iniKeyInfo();
newKey2.m_strKey=strKey;
newKey2.m_strValue=strValue;
newSection.m_Keys.Add(newKey2);
m_iniFile.Add(newSection);
WriteFile();
}
catch(Exception err)
{
throw err;
}
}
/// <summary>
/// 写某一节,某一键值
/// </summary>
/// <param name="strSection">某一节</param>
/// <param name="strKey">某一键</param>
/// <param name="strValue">键值</param>
/// <param name="strMemo">备注</param>
/// <returns>如果成功无返回值,否则抛出错误</returns>
public void WriteProfileString(string strSection,string strKey,string strValue,string strMemo)
{
try
{
foreach(iniSectionInfo section in m_iniFile)
{
if(section.m_strSection==strSection)
{
foreach(iniKeyInfo key in section.m_Keys)
{
if(key.m_strKey==strKey)
{
section.m_Keys.Remove(key);
iniKeyInfo newKey=new iniKeyInfo();
newKey.m_strKey=strKey;
newKey.m_strValue=strValue;
newKey.m_strMemo=";"+strMemo;
section.m_Keys.Add(newKey);
WriteFile();
return ;
}
}
iniKeyInfo newKey1=new iniKeyInfo();
newKey1.m_strKey=strKey;
newKey1.m_strValue=strValue;
newKey1.m_strMemo=";"+strMemo;
section.m_Keys.Add(newKey1);
WriteFile();
return;
}
}
iniSectionInfo newSection=new iniSectionInfo();
newSection.m_strSection=strSection;
newSection.m_Keys=new ArrayList();
iniKeyInfo newKey2=new iniKeyInfo();
newKey2.m_strKey=strKey;
newKey2.m_strValue=strValue;
newKey2.m_strMemo=";"+strMemo;
newSection.m_Keys.Add(newKey2);
m_iniFile.Add(newSection);
WriteFile();
}
catch(Exception err)
{
throw err;
}
}
/// <summary>
/// 写文件,成功无返回,失败抛出错误
/// </summary>
private void WriteFile()
{
try
{
StreamWriter sw=new StreamWriter(m_strFile,false,System.Text.Encoding.Unicode);
string strKey;
foreach(iniSectionInfo section in m_iniFile)
{
string strSection=string.Format("[{0}] {1}",section.m_strSection,section.m_strMemo);
sw.WriteLine(strSection);
foreach(iniKeyInfo key in section.m_Keys)
{
if(key.m_strKey==null)
strKey=string.Format("{0}",key.m_strMemo);
else
strKey=string.Format("{0} = {1} {2}",key.m_strKey,key.m_strValue,key.m_strMemo);
sw.WriteLine(strKey);
}
}
sw.Close();
}
catch(Exception err)
{
throw err;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -