ini文件操作

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace IniFile
{
    public class IniFileOS
    {
        //文件inif名称
        public string Path;
        //------------声明都写INI的API 函数
        //---写ini文件的api
        [DllImport("kernel32")]

        private static extern long WritePrivateProfileString
            (string section, string key, string val, string filePath);
        //section INI文件的段落 key 关键值 val 关键值的数值 filePath文件的完整路径和名称

        //-----读ini 的api
        [DllImport("kernel32")]

        private static extern int GetPrivateProfileString
            (string section, string key, string def, StringBuilder retVal, int size, string filePath);
        //def 无法读取时的缺省值 retVal读取数值 size数值大小

        //----------------本 类的构造函数 传递文件路径---
        public IniFileOS(string inipath)
        {
            Path = inipath;
        }

        //-----写入ini文件 的 方法------------依次 段落 关键值 数值
        public void iniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value,this.Path);
        }
        //-----读取ini文件 的方法
        public string iniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.Path);
            return temp.ToString();

        }
        // 删除段落
        public void delSection(string Section) 
        {
            WritePrivateProfileString(Section, null, null, this.Path);
        }
        // 删除key值
        public void delKey(string Section, string Key)
        {
            WritePrivateProfileString(Section, Key, null, this.Path);
        }

        //--验证文件是否存在
        public bool ExitIniFile()
        {
            return File.Exists(this.Path);
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值