已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C# 操作INI文件类private void testini_load(object sender, system.eventargs e)/创建一个inifile对象,参数为文件路径,如果不存在他会自动创建的inifile inf=new inifile(d:工作目录vsppvsppserverbindebugcompiled.ini);/显示ini设置的结构foreach(string k in inf.segments.keys)treenode o=new treenode(k);inisegment s=inf.segmentsk;/取出当前设置节foreach(string k1 in s.items.keys)treenode o1=new treenode(k1+ = +s.itemsk1.value);/访问设置节中每个设置项o.nodes.add(o1);treeview1.nodes.add(o);/添加一个设置inf.segments.add(test);/添加test节inf.segmentstest.items.add(ip,);/添加一个设置项/快速添加一个设置项inf.segments测试.itemshost.value=localhost;/ :)和上面两行相同的效果/读取也是同样的方便messagebox.show(inf.segments测试.itemshost.value);/如果不存在这样的设置项,将返回空字符串并创建这个项messagebox.show(inf.segments测试.itemsserver.value);/清除一个设置节下面的所有设置项/inf.segmentsaction.clear();再show代码:using system;using system.io;using system.data;using system.text;using system.collections;using system.text.regularexpressions;using eropservices;namespace eglic#region ini文件操作类/ / 设置节/ public class inisegmentprivate string _name;private inisegments _owner;/ / 所有设置项集合/ public iniitems items;/ / 构造函数/ / owner/ 设置节名称public inisegment(inisegments o,string vname)_owner=o;_name=vname;items=new iniitems(this);o.owner.getsegment(this);/ / 获取设置节的名称/ public string nameget return _name;/ / 获取segment的owner:inisegments集合/ public inisegments ownerget return _owner;/ / 清除所有设置项/ public void clear()_owner.owner.writesegment(_name,00);/ / 设置节集合/ public class inisegments : dictionarybaseprivate inifile _owner;/ / 构造函数/ / ownerpublic inisegments(inifile o)_owner=o;/ / 获取此对象的owner:inifile类/ public inifile ownerget return _owner;/ / 添加一个已存在的设置节/ / 设置节对象public void add(inisegment o)if(!dictionary.contains()dictionary.add(,o);/ / 添加一个可能不存在的设置节(创建一个设置节)/ / 设置节名称/ 添加的设置节public inisegment add(string vname)if(dictionary.contains(vname)return (inisegment)dictionaryvname;inisegment o=new inisegment(this,vname);dictionary.add(vname,o);return o;/ / 获取索引集合/ public icollection keysget return dictionary.keys;/ / 获取值集合/ public icollection valuesget return dictionary.values;/ / 获取设置节/ public inisegment this string vnamegetif(!dictionary.contains(vname)return this.add(vname);elsereturn (inisegment)dictionaryvname;/ / 获取是否包含某设置节/ / 设置节名称/ 是否public bool contains(string vname)return dictionary.contains(vname);/ / 设置项/ public class iniitemprivate string _name;private string _value;private iniitems _owner;/ / 构造函数/ / owner/ 名称/ 值public iniitem(iniitems o,string vname,string vvalue)_owner=o;_name=vname;_value=vvalue;if(!o.contains(vname)o.owner.owner.owner.setstring(,vname,vvalue);/ / 获取名称/ public string nameget return _name;/ / 获取设置值/ public string valueget return _value;set _value=value;_owner.owner.owner.owner.setstring(_,_name,value);/ / 获取owner/ public iniitems ownerget return _owner;/ / 设置项集合/ public class iniitems : dictionarybaseprivate inisegment _owner;/ / 构造函数/ / ownerpublic iniitems(inisegment o)_owner=o;/ / 获取owner/ public inisegment ownerget return _owner;/ / 添加一个已存在的设置项/ / 设置项public void add(iniitem o)if(!dictionary.contains()dictionary.add(,o);/ / 获取是否包含指定名称的设置项/ / 设置项名称/ 是否public bool contains(string vname)return dictionary.contains(vname);/ / 获取所有的索引集合/ public icollection keysget return dictionary.keys;/ / 获取所有的值集合/ public icollection valuesget return dictionary.values;/ / 添加一个可能不存在的设置项(创建一个设置项)/ / 设置项名/ 设置项值/ 创建的设置项iniitem对象public iniitem add(string vname,string vvalue)if(dictionary.contains(vname)return (iniitem)dictionaryvname;elseiniitem o=new iniitem(this,vname,vvalue);this.add(o);return o;/ / 获取指定索引的设置项/ public iniitem thisstring vnameget if(dictionary.contains(vname)return (iniitem)dictionaryvname;elsereturn this.add(vname,);/ / ini文件操作类/ public class inifile#region 导入dll函数dllimport(kernel32.dll) public extern static int getprivateprofileinta(string segname,string keyname,int idefault,string filename);dllimport(kernel32.dll) public extern static int getprivateprofilestringa(string segname,string keyname,string sdefault,stringbuilder retvalue,int nsize,string filename);dllimport(kernel32.dll) public extern static int getprivateprofilesectiona(string segname,byte sdata,int nsize,string filename);dllimport(kernel32.dll) public extern static int writeprivateprofilesectiona(string segname,byte sdata,string filename);dllimport(kernel32.dll) public extern static int writeprivateprofilestringa(string segname,string keyname,string svalue,string filename);dllimport(kernel32.dll) public extern static int getprivateprofilesectionnamesa(byte vdata,int ilen,string filename);#endregionprivate string _path;/ / 所有的设置节/ public inisegments segments;/ / 构造函数/ / ini文件路径public inifile(string vpath)_path=vpath;segments=new inisegments(this);byte bufsegs=new byte 32767;int rel=getprivateprofilesectionnamesa(bufsegs,32767,_path);int icnt,ipos;string tmp;if(rel0)icnt=0;ipos=0;for(icnt=0;icntrel;icnt+)if(bufsegsicnt=0x00)tmp=system.text.asciiencoding.default.getstring(bufsegs,ipos,icnt).trim();ipos=icnt+1;if(tmp!=)segments.add(tmp);/ / 获取ini文件路径/ public string pathget return _path;/ / 读取一个整数型的设置值/ / 设置节名/ 设置项名/ 默认值/ 设置值public int getint(string segname,string keyname,int idefault)return getprivateprofileinta(segname,keyname,idefault,_path);/ / 读取一个字符串型设置值/ / 设置节名/ 设置项名/ 默认值/ 设置值public string getstring(string segname,string keyname,string sdefault)stringbuilder red=new stringbuilder(1024);getprivateprofilestringa(segname,keyname,red,1024,_path);return red.tostring();/ / 写入设置项/ / 设置节名/ 设置项名/ 设置值public void setstring(string segname,string keyname,string vvalue)writeprivateprofilestringa(segname,keyname,vvalue,_path);/ / 写入一个设置节/ / 设置节名/ 数据/ / 数据为多个设置项组成的字符串,每个设置项之间用 0 分割/ 字符串最后用 00 结束/ / / writesegm
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年企业内部培训与人才培养策略指南
- 网络安全态势感知与防护策略指南(标准版)
- 银行间市场业务操作指南
- 一次难忘的徒步经历记事作文(15篇)
- 销售合同审核与管理标准化流程模板
- 水利水电工程招标投标管理办法
- 2025年企业信息安全管理流程手册
- 市场营销策略实施效果评估与反馈表
- 人力资源管理师实操手册(标准版)
- 2026年黄山市屯溪区消防救援局面向社会公开招聘工作人员备考题库及答案详解参考
- 财务审计工作程序及风险防范措施
- (人力资源管理专科)毕业论文
- 刮板链式运输机三级圆锥齿轮减速器设计
- 解读(2025年版)输卵管积水造影诊断中国专家共识
- 创新中心人员管理制度
- (正式版)DB50∕T 1879-2025 《刨猪宴菜品烹饪技术规范》
- 高职院校技能大赛指导手册
- 轨道交通废弃物处理-洞察及研究
- 2024年一建网络图案例专题
- 智齿拔除术课件
- DG-TJ08-401-2025 公共厕所规划和设计标准
评论
0/150
提交评论