2022年c#读取ini文件封装类 .pdf

上传人:Che****ry 文档编号:34878262 上传时间:2022-08-19 格式:PDF 页数:6 大小:43.08KB
返回 下载 相关 举报
2022年c#读取ini文件封装类 .pdf_第1页
第1页 / 共6页
2022年c#读取ini文件封装类 .pdf_第2页
第2页 / 共6页
点击查看更多>>
资源描述

《2022年c#读取ini文件封装类 .pdf》由会员分享,可在线阅读,更多相关《2022年c#读取ini文件封装类 .pdf(6页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、C# 对 Ini 文件操作( C# ini 文件操作类)2009-08-15 19:42/* = C# 对 Ini 文件操作( C# ini 文件操作类)IniFiles.cs = =蝶晓梦整理了好久,拿出来分享注意,不要用相对路径,要用绝对路径,否则有几个函数会把文件建立到“C:Windows ”目录下如果找不到建立的文件,很可能建立到“C:Windows ”目录下了代码是蝶晓梦从网上收集的整理而成,改动并且增加了一些,感谢以前写这些代码的人使用方法:添加到工程文件的引用现有项里面,然后在想用的地方这样用:#region 读写配置文件string AppPath= Path.GetDirec

2、toryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)+; string IniFile=MyIniFile.ini; IniFiles Ini = new IniFiles(AppPath+IniFile);/ini文件的绝对路径Ini.WriteV alue(RunTime Control, Running, mystring);#endregion 蝶晓梦说这个类能干很多事情,比如增加一行注释Ini.AddNotes(This is a note); 比如写入一个字符串的值Ini.Write

3、V alue(RunTime Control, Running, mystring);比如写入一个整型的值Ini.WriteV alue(RunTime Control, Running, 0); 比如写入一个布尔型的值Ini.WriteV alue(RunTime Control, Running, true); 还能写入datetime 的和 object 的读的时候能读字符串的,整型的,布尔的,datetime 的,例如bool running=Ini.ReadV alue(RunTime Control, Running, true); 其他功能自己可以增加或者发掘写了两个事件,一个是

4、当向Ini.FileName 赋值时会触发Ini 文件改变的事件还有一个是这个类实例化的时候会触发一个举例子:Ini.IniFileChanged+=new IniFiles.EventHandler(Ini_IniFileChanged);/注册事件然后事件触发的内容写在这里void Ini_IniFileChanged(object sender, EventArgs e) /写代码于此 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 6 页 - - - - - -

5、- - - 蝶晓梦说,大家以后可以增加这个类的功能,使这个类更加强大增加内容:蝶晓梦说,平时不要首先用addnotes 和 addtext,可能会因为文本编码的问题出错= */ using System; using System.IO; using System.Text; using System.Runtime.InteropServices; public class IniFiles public delegate void EventHandler(object sender, EventArgs e); public event EventHandler IniFileChang

6、ed; public event EventHandler Initialization; protected string IniFileName; public string FileName get return IniFileName; set if (value != IniFileName) IniFileName = value; OnIniFileChanged(new EventArgs(); protected void OnIniFileChanged(EventArgs e) if (IniFileChanged != null) IniFileChanged(null

7、, e); protected void OnInitialization(EventArgs e) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 6 页 - - - - - - - - - if (Initialization != null) Initialization(null, e); DllImport(kernel32) private static extern long WritePrivateProfileString(string section,

8、 string key, string val, string filePath); /* section: 要写入的段落名key: 要写入的键,如果该key 存在则覆盖写入val: key 所对应的值filePath: INI 文件的完整路径和文件名*/ DllImport(kernel32) private static extern int GetPrivateProfileString(string section, string key, string defVal, System.Text.StringBuilder retV al, int size, string filePa

9、th); /* section:要读取的段落名key: 要读取的键defVal: 读取异常的情况下的缺省值retVal: key 所对应的值,如果该key 不存在则返回空值size: 值允许的大小filePath: INI 文件的完整路径和文件名*/ / / 构造方法/ / 文件路径 public IniFiles(string FileName) IniFileName = FileName; / / 写入 INI 文件/ / 项目名称 (如 TypeName ) / 键 / 值 public void WriteV alue(string Section, string Key, stri

10、ng V alue) WritePrivateProfileString(Section, Key, V alue, this.IniFileName); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 6 页 - - - - - - - - - / / 读出 INI 文件/ / 项目名称 (如 TypeName ) / 键 public string ReadValue(string Section, string Key ,string Default) String

11、Builder temp = new StringBuilder(500); int i = GetPrivateProfileString(Section, Key, Default, temp, 500, this.IniFileName); return temp.ToString(); / / 验证文件是否存在/ / 布尔值 public bool ExistINIFile() return File.Exists(IniFileName); / / 创建文件夹/ / 路径 private void NewDirectory(String path) if (!Directory.Ex

12、ists(path) Directory.CreateDirectory(path); / / 添加一行注释/ / 注释 public void AddNotes(string Notes) string filename = IniFileName; string path; path = Directory .GetParent(filename).ToString(); NewDirectory(path); FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write); StreamW

13、riter sw = new StreamWriter(fs); sw.BaseStream.Seek(0, SeekOrigin.End); sw.WriteLine(; + Notes); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 6 页 - - - - - - - - - sw.Flush(); sw.Close(); fs.Close(); sw.Dispose(); fs.Dispose(); / / 添加一行文本/ / 文本 public void Ad

14、dText(string Text) string filename = IniFileName; string path; path = Directory .GetParent(filename).ToString(); NewDirectory(path); FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.BaseStream.Seek(0, SeekOrigin.End); sw.Wr

15、iteLine(Text); sw.Flush(); sw.Close(); fs.Close(); sw.Dispose(); fs.Dispose(); #region 重载public void WriteV alue(string Section, string Key, int V alue) WriteV alue(Section, Key, Value.ToString(); public void WriteV alue(string Section, string Key, Boolean V alue) WriteV alue(Section, Key, Value.ToS

16、tring(); public void WriteV alue(string Section, string Key, DateTime V alue) WriteV alue(Section, Key, Value.ToString(); public void WriteV alue(string Section, string Key, object V alue) WriteV alue(Section, Key, Value.ToString(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - -

17、 - - - - - 第 5 页,共 6 页 - - - - - - - - - public int ReadValue(string Section, string Key, int Default) return Convert.ToInt32(ReadValue(Section, Key, Default.ToString(); public bool ReadValue(string Section, string Key, bool Default) return Convert.ToBoolean(ReadV alue(Section, Key, Default.ToString

18、(); public DateTime ReadValue(string Section, string Key, DateTime Default) return Convert.ToDateTime(ReadV alue(Section, Key, Default.ToString(); public string ReadValue(string Section, string Key) return ReadValue(Section, Key, ); #endregion /* 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 6 页 - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com