首页 > 编程语言 >[CSharpTips]C# 读写INI文件

[CSharpTips]C# 读写INI文件

时间:2022-09-01 14:55:06浏览次数:53  
标签:string filePath C# section int static CSharpTips INI

读写.ini文件工具 可以直接使用

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

namespace WinFormSharpDemo.Common.Helpers
{
    /// <summary>
    /// INI文件读写工具
    /// </summary>
    public static class IniHelper
    {
        /// <summary>
        /// 为INI文件中指定的节点取得字符串
        /// </summary>
        /// <param name="lpAppName">欲在其中查找关键字的节点名称</param>
        /// <param name="lpKeyName">欲获取的项名</param>
        /// <param name="lpDefault">指定的项没有找到时返回的默认值</param>
        /// <param name="lpReturnedString">指定一个字串缓冲区,长度至少为nSize</param>
        /// <param name="nSize">指定装载到lpReturnedString缓冲区的最大字符数量</param>
        /// <param name="lpFileName">INI文件完整路径</param>
        /// <returns>复制到lpReturnedString缓冲区的字节数量,其中不包括那些NULL中止字符</returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);

        /// <summary>
        /// 修改INI文件中内容
        /// </summary>
        /// <param name="lpApplicationName">欲在其中写入的节点名称</param>
        /// <param name="lpKeyName">欲设置的项名</param>
        /// <param name="lpString">要写入的新字符串</param>
        /// <param name="lpFileName">INI文件完整路径</param>
        /// <returns>非零表示成功,零表示失败</returns>
        [DllImport("kernel32")]
        private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);


        /// <summary>
        /// 获取所有节点
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="def"></param>
        /// <param name="retVal"></param>
        /// <param name="size"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]

        private static extern uint GetPrivateProfileStringA(string section, string key, string def, Byte[] retVal, int size, string filePath);

        /// <summary>
        /// 获取所有节点
        /// </summary>
        /// <param name="iniFilename">文件名</param>
        /// <returns></returns>
        public static List<string> ReadSections(string iniFilename)
        {
            List<string> result = new List<string>();
            Byte[] buf = new Byte[65536];
            uint len = GetPrivateProfileStringA(null, null, null, buf, buf.Length, iniFilename);
            int j = 0;
            for (int i = 0; i < len; i++)
                if (buf[i] == 0)
                {
                    result.Add(Encoding.Default.GetString(buf, j, i - j));
                    j = i + 1;
                }
            return result;
        }

        /// <summary>
        /// 读取INI文件值
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="key">键</param>
        /// <param name="def">未取到值时返回的默认值</param>
        /// <param name="filePath">INI文件完整路径</param>
        /// <returns>读取的值</returns>
        public static string Read(string section, string key, string def, string filePath)
        {
            StringBuilder sb = new StringBuilder(1024);
            GetPrivateProfileString(section, key, def, sb, 1024, filePath);
            return sb.ToString();
        }

        /// <summary>
        /// 写INI文件值
        /// </summary>
        /// <param name="section">欲在其中写入的节点名称</param>
        /// <param name="key">欲设置的项名</param>
        /// <param name="value">要写入的新字符串</param>
        /// <param name="filePath">INI文件完整路径</param>
        /// <returns>非零表示成功,零表示失败</returns>
        public static int Write(string section, string key, string value, string filePath)
        {
            if (!Directory.Exists(Path.GetDirectoryName(filePath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(filePath));
            }
            return WritePrivateProfileString(section, key, value, filePath);
        }

        /// <summary>
        /// 删除节
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="filePath">INI文件完整路径</param>
        /// <returns>非零表示成功,零表示失败</returns>
        public static int DeleteSection(string section, string filePath)
        {
            return Write(section, null, null, filePath);
        }

        /// <summary>
        /// 删除键的值
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="key">键名</param>
        /// <param name="filePath">INI文件完整路径</param>
        /// <returns>非零表示成功,零表示失败</returns>
        public static int DeleteKey(string section, string key, string filePath)
        {
            return Write(section, key, null, filePath);
        }
    }
}

 

标签:string,filePath,C#,section,int,static,CSharpTips,INI
From: https://www.cnblogs.com/axiaoshuye/p/16435158.html

相关文章

  • 给Docker集群中Label节点打上标签与服务约束
    https://www.cnblogs.com/caoweixiong/p/12382282.htmlLabel作用:在服务器中通常需要将某个服务固定在某一台机器上运行的时候,可以给集群中的机器打上标签......
  • JavaDOC
    示例代码/***@authorMMF*@version1.0*@since1.8*/publicclassDoc{Stringname;/***@authorMMF*@paramname用户名*......
  • Tomcat安装及环境变量配置
    Tomcat下载及安装Step①:进入Tomcat官网:WelcometoTheApacheSoftwareFoundation! 首字母T的指引下,找到Tomcat。 Download下边的版本,这里最新的是10.x版本,但是......
  • logback.xml 配置文件
    logback.xml<?xmlversion="1.0"encoding="UTF-8"?><configuration><!--定义日志文件的存储地址勿在LogBack的配置中使用相对路径--><!--<propertynam......
  • CentOS搭建饥荒服务器
    我用的系统为CentOS7.6 1.安装部署服务器需要的环境yum-yupdate#升级服务器yum-yinstallscreenglibc.i686libstdc++.i686libcurl.i686#安装所需环境2.......
  • 如何理解CV_8UC3等表达式
    出处:http://www.cnblogs.com/suubai/OpenCV在对Mat对象进行初始化时,经常需要对矩阵的类型进行定义,这就需要用到类似于CV_8UC3的表达。该宏定义的通式为: 1、bit_depth:......
  • 记一次feign调用报错:feign.codec.DecodeException: Error while extracting response
    一直以为是被调用法的返回对象类型和调用方接收的对象类型不一致导致的解析失败甚至以为无法传递除Jsondate以外的类型 实际问题:多服务调用导出用到多线程token无法......
  • 使用 kubectl patch 修改 Kubernetes objects
    为了能够修改Kubernetes对象,我们可以使用kubectledit以交互方式进行修改。如果我们需要测试值,它可以派上用场,但它使自动化变得更加困难。如果我们需要一种使用非交互式命......
  • Docker镜像构建
    Docker镜像构建目录Docker镜像构建Dockercommit基于本地模板导入Dockerfile​ Docker镜像可以通过Dockerhub或者阿里云等仓库中获取,这些镜像是由官方或者社区人员提供......
  • JavaScript高级程序设计(第3版) pdf
    高清扫描版下载链接:https://pan.baidu.com/s/1rWAAzlVrJLfwXEn_SWtBWw点击这里获取提取码JavaScript高级程序设计本书从最早期Netscape浏览器中的JavaScript开始讲起,直到......