asp.net 操作INI配置文件类

简介: using System;using System.Collections.Generic;using System.Linq;using System.Web;using System;using System.Runtime.InteropServices;using System.Text;using System.IO;/// /// INI文件读写类。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;

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


相关文章
|
3月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
42 0
|
8月前
|
开发框架 前端开发 .NET
ASP.NET Core 基础知识之​Startup 类配置
Startup 类配置服务和应用的请求管道。
113 0
|
存储 开发框架 NoSQL
ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
284 0
ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
|
开发框架 JavaScript 前端开发
ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作
ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作
332 0
ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作
|
开发框架 前端开发 安全
ASP.NET Core Startup类Config gure()方法|ASP.NET Core 中间件详细说明
目录 Startup 类 Configure() 方法 中间件 使用中间件 Configure 方法 的参数 IApplicationBuilder Extension Methods(拓展方法)--微软提供的中间件
147 0
ASP.NET Core Startup类Config gure()方法|ASP.NET Core 中间件详细说明
|
XML 开发框架 JSON
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(五)
上一章的结尾留下了一个问题:同样是ObjectResult,在执行的时候又是如何被转换成string和JSON两种格式的呢? 本章来解答这个问题,这里涉及到一个名词:“内容协商”。除了这个,本章将通过两个例子来介绍如何自定义IActionResult和格式化类。
194 0
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(五)
|
JSON 开发框架 .NET
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(四)
上一章的结尾留下了一个问题:同样是ObjectResult,在执行的时候又是如何被转换成string和JSON两种格式的呢? 本章来解答这个问题,这里涉及到一个名词:“内容协商”。除了这个,本章将通过两个例子来介绍如何自定义IActionResult和格式化类。
134 0
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(四)
|
JSON 开发框架 .NET
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(三)
上一章的结尾留下了一个问题:同样是ObjectResult,在执行的时候又是如何被转换成string和JSON两种格式的呢? 本章来解答这个问题,这里涉及到一个名词:“内容协商”。除了这个,本章将通过两个例子来介绍如何自定义IActionResult和格式化类。
323 0
|
JSON 开发框架 .NET
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(二)
上一章的结尾留下了一个问题:同样是ObjectResult,在执行的时候又是如何被转换成string和JSON两种格式的呢? 本章来解答这个问题,这里涉及到一个名词:“内容协商”。除了这个,本章将通过两个例子来介绍如何自定义IActionResult和格式化类。
251 0
|
JSON 开发框架 .NET
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(一)
上一章的结尾留下了一个问题:同样是ObjectResult,在执行的时候又是如何被转换成string和JSON两种格式的呢? 本章来解答这个问题,这里涉及到一个名词:“内容协商”。除了这个,本章将通过两个例子来介绍如何自定义IActionResult和格式化类。
273 0
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(一)