苏飞论坛

标题: C# INIFileHelper文件读写类与源码下载 [打印本页]

作者: 站长苏飞    时间: 2014-1-13 09:07
标题: C# INIFileHelper文件读写类与源码下载
C# INI文件读写类与源码下载
导读部分
-------------------------------------------------------------------------------------------------------------
C#基类|C#自定义类|C#帮助类--系列导航文章
http://www.sufeinet.com/thread-655-1-1.html

源码下载,请到基库中直接查找http://www.sufeinet.com/thread-655-1-1.html

预览源码
[C#] 纯文本查看 复制代码
/// <summary>
/// 类说明:INI文件读写类。
/// 编 码 人:苏飞
/// 联系方式:361983679  
/// 更新网站:http://www.sufeinet.com/thread-655-1-1.html
/// </summary>
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace DotNet.Utilities
{
    /// <summary>
    /// INI文件读写类。
    /// </summary>
        public class INIFileHelper
        {
                public string path;

        public INIFileHelper(string INIPath)
                {
                        path = INIPath;
                }

                [DllImport("kernel32")]
                private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);

                [DllImport("kernel32")]
                private static extern int GetPrivateProfileString(string section,string key,string def, StringBuilder retVal,int size,string filePath);

        
                [DllImport("kernel32")]
                private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);


                /// <summary>
                /// 写INI文件
                /// </summary>
                /// <param name="Section"></param>
                /// <param name="Key"></param>
                /// <param name="Value"></param>
                public void IniWriteValue(string Section,string Key,string Value)
                {
                        WritePrivateProfileString(Section,Key,Value,this.path);
                }

                /// <summary>
                /// 读取INI文件
                /// </summary>
                /// <param name="Section"></param>
                /// <param name="Key"></param>
                /// <returns></returns>
                public string IniReadValue(string Section,string Key)
                {
                        StringBuilder temp = new StringBuilder(255);
                        int i = GetPrivateProfileString(Section,Key,"",temp, 255, this.path);
                        return temp.ToString();
                }
                public byte[] IniReadValues(string section, string key)
                {
                        byte[] temp = new byte[255];
                        int i = GetPrivateProfileString(section, key, "", temp, 255, this.path);
                        return temp;

                }


                /// <summary>
                /// 删除ini文件下所有段落
                /// </summary>
                public void ClearAllSection()
                {
                        IniWriteValue(null,null,null);
                }
                /// <summary>
                /// 删除ini文件下personal段落下的所有键
                /// </summary>
                /// <param name="Section"></param>
                public void ClearSection(string Section)
                {
                        IniWriteValue(Section,null,null);
                }

        }


}


作者: moulove    时间: 2014-3-30 20:59
受教了,学习中……
作者: 稻香老农    时间: 2014-7-28 12:24
不错,站长
作者: 隨風往事    时间: 2014-8-15 15:33
受教了,学习中……




欢迎光临 苏飞论坛 (http://www.sufeinet.com/) Powered by Discuz! X3.4