C#操作INI文件的辅助类IniHelper
使用INI配置文件,简单便捷。
该辅助工具类为C#操作INI文件的辅助类,源码在某位师傅的基础上完善的来,因为忘记最初的来源了,因此不能提及引用,在此深感遗憾,并对贡献者表示感谢。
usingSystem; usingSystem.Collections; usingSystem.Collections.Generic; usingSystem.Runtime.InteropServices; usingSystem.Text; namespaceEyuan.Common { publicstaticclassINIHelper { #region读写INI文件相关 [DllImport("kernel32.dll",EntryPoint="WritePrivateProfileString",CharSet=CharSet.Ansi)] privatestaticexternlongWritePrivateProfileString(stringsection,stringkey,stringval,stringfilePath); [DllImport("kernel32.dll",EntryPoint="GetPrivateProfileString",CharSet=CharSet.Ansi)] privatestaticexternintGetPrivateProfileString(stringsection,stringkey,stringdef,StringBuilderretVal,intsize,stringfilePath); [DllImport("kernel32")] privatestaticexternintGetPrivateProfileInt(stringlpApplicationName,stringlpKeyName,intnDefault,stringlpFileName); [DllImport("kernel32.dll",EntryPoint="GetPrivateProfileSectionNames",CharSet=CharSet.Ansi)] privatestaticexternintGetPrivateProfileSectionNames(IntPtrlpszReturnBuffer,intnSize,stringfilePath); [DllImport("KERNEL32.DLL",EntryPoint="GetPrivateProfileSection",CharSet=CharSet.Ansi)] privatestaticexternintGetPrivateProfileSection(stringlpAppName,byte[]lpReturnedString,intnSize,stringfilePath); #endregion #region读写操作(字符串) //////向INI写入数据 /// ///节点名 /// 键名 /// 值(字符串) publicstaticvoidWrite(stringSection,stringKey,stringValue,stringpath) { WritePrivateProfileString(Section,Key,Value,path); } /// ///读取INI数据 /// ///节点名 /// 键名 /// 值名 /// 值(字符串) publicstaticstringRead(stringSection,stringKey,stringpath) { StringBuildertemp=newStringBuilder(255); inti=GetPrivateProfileString(Section,Key,"",temp,255,path); returntemp.ToString(); } #endregion #region配置节信息 //////读取一个ini里面所有的节 /// ////// /// -1:没有节信息,0:正常 publicstaticintGetAllSectionNames(outstring[]sections,stringpath) { intMAX_BUFFER=32767; IntPtrpReturnedString=Marshal.AllocCoTaskMem(MAX_BUFFER); intbytesReturned=GetPrivateProfileSectionNames(pReturnedString,MAX_BUFFER,path); if(bytesReturned==0) { sections=null; return-1; } stringlocal=Marshal.PtrToStringAnsi(pReturnedString,(int)bytesReturned).ToString(); Marshal.FreeCoTaskMem(pReturnedString); //useofSubstringbelowremovesterminatingnullforsplit sections=local.Substring(0,local.Length-1).Split('\0'); return0; } //////返回指定配置文件下的节名称列表 /// ////// publicstaticList GetAllSectionNames(stringpath) { List sectionList=newList (); intMAX_BUFFER=32767; IntPtrpReturnedString=Marshal.AllocCoTaskMem(MAX_BUFFER); intbytesReturned=GetPrivateProfileSectionNames(pReturnedString,MAX_BUFFER,path); if(bytesReturned!=0) { stringlocal=Marshal.PtrToStringAnsi(pReturnedString,(int)bytesReturned).ToString(); Marshal.FreeCoTaskMem(pReturnedString); sectionList.AddRange(local.Substring(0,local.Length-1).Split('\0')); } returnsectionList; } /// ///得到某个节点下面所有的key和value组合 /// ///指定的节名称 /// Key数组 /// Value数组 /// INI文件路径 /// publicstaticintGetAllKeyValues(stringsection,outstring[]keys,outstring[]values,stringpath) { byte[]b=newbyte[65535];//配置节下的所有信息 GetPrivateProfileSection(section,b,b.Length,path); strings=System.Text.Encoding.Default.GetString(b);//配置信息 string[]tmp=s.Split((char)0);//Key\Value信息 List result=newList (); foreach(stringrintmp) { if(r!=string.Empty) result.Add(r); } keys=newstring[result.Count]; values=newstring[result.Count]; for(inti=0;i 2) { keys[i]=item[0].Trim(); values[i]=result[i].Substring(keys[i].Length+1); } if(item.Length==2)//Key=Value { keys[i]=item[0].Trim(); values[i]=item[1].Trim(); } elseif(item.Length==1)//Key= { keys[i]=item[0].Trim(); values[i]=""; } elseif(item.Length==0) { keys[i]=""; values[i]=""; } } return0; } /// ///得到某个节点下面所有的key /// ///指定的节名称 /// Key数组 /// INI文件路径 /// publicstaticintGetAllKeys(stringsection,outstring[]keys,stringpath) { byte[]b=newbyte[65535]; GetPrivateProfileSection(section,b,b.Length,path); strings=System.Text.Encoding.Default.GetString(b); string[]tmp=s.Split((char)0); ArrayListresult=newArrayList(); foreach(stringrintmp) { if(r!=string.Empty) result.Add(r); } keys=newstring[result.Count]; for(inti=0;i ///获取指定节下的Key列表 /// /// 指定的节名称 /// 配置文件名称 /// Key列表 publicstaticListGetAllKeys(stringsection,stringpath) { List keyList=newList (); byte[]b=newbyte[65535]; GetPrivateProfileSection(section,b,b.Length,path); strings=System.Text.Encoding.Default.GetString(b); string[]tmp=s.Split((char)0); List result=newList (); foreach(stringrintmp) { if(r!=string.Empty) result.Add(r); } for(inti=0;i ///获取值 /// /// /// /// publicstaticList GetAllValues(stringsection,stringpath) { List keyList=newList (); byte[]b=newbyte[65535]; GetPrivateProfileSection(section,b,b.Length,path); strings=System.Text.Encoding.Default.GetString(b); string[]tmp=s.Split((char)0); List result=newList (); foreach(stringrintmp) { if(r!=string.Empty) result.Add(r); } for(inti=0;i ///第一个键 /// /// /// /// /// publicstaticstringGetFirstKeyByValue(stringsection,stringpath,stringvalue) { foreach(stringkeyinGetAllKeys(section,path)) { if(ReadString(section,key,"",path)==value) { returnkey; } } returnstring.Empty; } /// ///所有键 /// ////// /// /// publicstaticList GetKeysByValue(stringsection,stringpath,stringvalue) { List keys=newList (); foreach(stringkeyinGetAllKeys(section,path)) { if(ReadString(section,key,"",path)==value) { keys.Add(key); } } returnkeys; } #endregion #region具体类型的读写 #regionstring /// /// /// ////// /// /// /// publicstaticstringReadString(stringsectionName,stringkeyName,stringdefaultValue,stringpath) { constintMAXSIZE=255; StringBuildertemp=newStringBuilder(MAXSIZE); GetPrivateProfileString(sectionName,keyName,defaultValue,temp,255,path); returntemp.ToString(); } /// /// /// ////// /// /// publicstaticvoidWriteString(stringsectionName,stringkeyName,stringvalue,stringpath) { WritePrivateProfileString(sectionName,keyName,value,path); } #endregion #regionInt /// /// /// ////// /// /// /// publicstaticintReadInteger(stringsectionName,stringkeyName,intdefaultValue,stringpath) { returnGetPrivateProfileInt(sectionName,keyName,defaultValue,path); } /// /// /// ////// /// /// publicstaticvoidWriteInteger(stringsectionName,stringkeyName,intvalue,stringpath) { WritePrivateProfileString(sectionName,keyName,value.ToString(),path); } #endregion #regionbool /// ///读取布尔值 /// ////// /// /// /// publicstaticboolReadBoolean(stringsectionName,stringkeyName,booldefaultValue,stringpath) { inttemp=defaultValue?1:0; intresult=GetPrivateProfileInt(sectionName,keyName,temp,path); return(result==0?false:true); } /// ///写入布尔值 /// ////// /// /// publicstaticvoidWriteBoolean(stringsectionName,stringkeyName,boolvalue,stringpath) { stringtemp=value?"1":"0"; WritePrivateProfileString(sectionName,keyName,temp,path); } #endregion #endregion #region删除操作 /// ///删除指定项 /// ////// /// publicstaticvoidDeleteKey(stringsectionName,stringkeyName,stringpath) { WritePrivateProfileString(sectionName,keyName,null,path); } /// ///删除指定节下的所有项 /// ////// publicstaticvoidEraseSection(stringsectionName,stringpath) { WritePrivateProfileString(sectionName,null,null,path); } #endregion #region判断节、键是否存在 /// ///指定节知否存在 /// ////// /// publicstaticboolExistSection(stringsection,stringfileName) { string[]sections=null; GetAllSectionNames(outsections,fileName); if(sections!=null) { foreach(varsinsections) { if(s==section) { returntrue; } } } returnfalse; } /// ///指定节下的键是否存在 /// ////// /// /// publicstaticboolExistKey(stringsection,stringkey,stringfileName) { string[]keys=null; GetAllKeys(section,outkeys,fileName); if(keys!=null) { foreach(varsinkeys) { if(s==key) { returntrue; } } } returnfalse; } #endregion #region同一Section下添加多个Key\Value /// /// /// ////// /// /// /// publicstaticboolAddSectionWithKeyValues(stringsection,List keyList,List valueList,stringpath) { boolbRst=true; //判断Section是否已经存在,如果存在,返回false //已经存在,则更新 //if(GetAllSectionNames(path).Contains(section)) //{ //returnfalse; //} //判断keyList中是否有相同的Key,如果有,返回false //添加配置信息 for(inti=0;i 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。