INI文件读写类代码详细介绍

来源:爱站网时间:2022-06-01编辑:网友分享
本文来给大家说说看INI文件读写类代码详细介绍,为了让大家更好的理解这篇内容,爱站技术频道小编整理了以下相关内容,如果你有需要请自行参考阅读,一定能在工作中帮助到你解决问题。
复制代码 代码如下:
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;

namespace Common
{
/// <summary>
/// INI文件读写类。
/// </summary>
    public class INIFile
    {
        public string path;

        public INIFile(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);
        } 
    }
}

 

INI文件读写类代码详细介绍想必朋友们都看明白了吧!如果觉得有哪些方面需要补充的,可以随时来网站给小编留言。更多精彩的技术文章,尽在js.aizhan.com。

上一篇:asp.net怎么进行身份验证

下一篇:asp.net url重写组件的介绍

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载