欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net INI文件读写类

程序员文章站 2024-03-08 22:32:22
复制代码 代码如下:using system; using system.runtime.interopservices; using system.text; using...
复制代码 代码如下:

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);
        } 
    }
}

没有太多含量,做雕虫小技是还是用得上。