手机版 | 登陆 | 注册 | 留言 | 设首页 | 加收藏
当前位置: 网站首页 > 电脑程序 > 编程技巧 > C# > 文章 当前位置: C# > 文章

C# 操作INI文件的类

时间:2018-10-18    点击: 次    来源:网络    作者:佚名 - 小 + 大

using System;using System.IO;using System.Text;using System.Runtime.InteropServices;namespace IniFilesOption{    class IniFiles    {        public delegate void EventHandler(object sender, EventArgs e);        public event EventHandler IniFileChanged;        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, e);        }        protected void OnInitialization(EventArgs e)        {            if (Initialization != null)                Initialization(null, e);        }        [DllImport("kernel32")]        private static extern long WritePrivateProfileString(string section, 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 retVal, int size, string filePath);        /*        section:要读取的段落名        key: 要读取的键        defVal: 读取异常的情况下的缺省值        retVal: key所对应的值,如果该key不存在则返回空值        size: 值允许的大小        filePath: INI文件的完整路径和文件名        */        /// <summary>        /// 构造方法        /// </summary>        /// <param name="INIPath">文件路径</param>        public IniFiles(string FileName)        {            IniFileName = FileName;        }        /// <summary>        /// 写入INI文件        /// </summary>        /// <param name="Section">项目名称(如 [TypeName] )</param>        /// <param name="Key">键</param>        /// <param name="Value">值</param>        public void WriteValue(string Section, string Key, string Value)        {            WritePrivateProfileString(Section, Key, Value, this.IniFileName);        }        /// <summary>        /// 读出INI文件        /// </summary>        /// <param name="Section">项目名称(如 [TypeName] )</param>        /// <param name="Key">键</param>        public string ReadValue(string Section, string Key, string Default)        {            StringBuilder temp = new StringBuilder(1500);            int i = GetPrivateProfileString(Section, Key, Default, temp, 1500, this.IniFileName);            return temp.ToString();        }        /// <summary>        /// 验证文件是否存在        /// </summary>        /// <returns>布尔值</returns>        public bool ExistINIFile()        {            return File.Exists(IniFileName);        }        /// <summary>        /// 创建文件夹        /// </summary>        /// <param name="path">路径</param>        private void NewDirectory(String path)        {            if (!Directory.Exists(path))            {                Directory.CreateDirectory(path);            }        }        /// <summary>        /// 添加一行注释        /// </summary>        /// <param name="Notes">注释</param>        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);            StreamWriter sw = new StreamWriter(fs);            sw.BaseStream.Seek(0, SeekOrigin.End);            sw.WriteLine(@";" + Notes);            sw.Flush();            sw.Close();            fs.Close();            sw.Dispose();            fs.Dispose();        }        /// <summary>        /// 添加一行文本        /// </summary>        /// <param name="Text">文本</param>        public void AddText(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.WriteLine(Text);            sw.Flush();            sw.Close();            fs.Close();            sw.Dispose();            fs.Dispose();        }        #region 重载        public void WriteValue(string Section, string Key, int Value)        {            WriteValue(Section, Key, Value.ToString());        }        public void WriteValue(string Section, string Key, Boolean Value)        {            WriteValue(Section, Key, Value.ToString());        }        public void WriteValue(string Section, string Key, DateTime Value)        {            WriteValue(Section, Key, Value.ToString());        }        public void WriteValue(string Section, string Key, object Value)        {            WriteValue(Section, Key, Value.ToString());        }        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(ReadValue(Section, Key, Default.ToString()));        }        public DateTime ReadValue(string Section, string Key, DateTime Default)        {            return Convert.ToDateTime(ReadValue(Section, Key, Default.ToString()));        }        public string ReadValue(string Section, string Key)        {            return ReadValue(Section, Key, "");        }        #endregion    }}
在VS2010上测试通过

上一篇:C#wenbbrowser浏览器的使用方法

下一篇:DataTable的Select的使用

免责声明 | 联系我们 | 关于我们
蜀ICP备17040878号-1  |   QQ:57758813  |  地址:成都市新都区  |  电话:12345678910  |  
Copyright © 2019 天人文章管理系统 版权所有,授权www.yshwang.cn使用 Powered by 55TR.COM