开发接口程序时,对于接口程序配置的IP地址、端口等都需要是可配置的,而在Win Api原生实现了INI文件的读写操作,因此只需要调用Win Api中的方法即可操作INI配置文件,关键代码就是如何调用Win Api中的方法,如下所示:

#region 调用WinApi 原方法声明
        [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);
#endregion

具体代码如下所示(删除段落内容是参考苏飞论坛苏飞大神的):

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace DotNetCommon.File
{

    /// <summary>
    /// 类说明:INI文件读写类
    /// 编码人:鞠小军
    /// 联系方式:binghuojxj@qq.com
    /// </summary>
    public class IniFileHelper
    {
        /// <summary>
        /// INI文件路径
        /// </summary>
        public string Path;
        /// <summary>
        /// 屏蔽空的构造函数
        /// </summary>
        public IniFileHelper()
        {
            throw new Exception("不允许使用空的构造函数!");
        }
        /// <summary>
        /// 构造函数,参数为INI文件路径
        /// </summary>
        /// <param name="path">INI文件的路径</param>
        public IniFileHelper(string path)
        {
            Path = path;
        }
        #region 调用WinApi 原方法声明
        [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);
        #endregion
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="section">段落</param>
        /// <param name="key">键</param>
        /// <returns></returns>
        public string IniReadValue(string section, string key)
        {
            );
            , Path);
            return temp.ToString();
        }

        /// <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, Path);
        }
        /// <summary>
        /// 清楚INI文件中所有的段落
        /// </summary>
        public void ClearAllSection()
        {
            IniWriteValue(null, null, null);
        }

        /// <summary>
        /// 清楚INI文件中指定段落内容
        /// </summary>
        /// <param name="section">段落</param>
        public void ClearSection(string section)
        {
            IniWriteValue(section, null, null);
        }

    }
}

【个人使用.Net类库】(1)INI配置文件操作类的更多相关文章

  1. C# ini配置文件操作类

    /// <summary> /// INI文件操作类 /// </summary> public class IniFileHelper { /// <summary&g ...

  2. Ini文件操作类

    /// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...

  3. C# 配置文件操作类

    注意添加引用:System.Configuration: using System; using System.Collections.Generic; using System.Text; usin ...

  4. Ini配置文件操作

    package cn.com.szhtkj.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOE ...

  5. C# INI配置文件读写类

    ini是一种很古老的配置文件,C#操作ini文件借助windows底层ini操作函数,使用起来很方便: public class IniHelper { [DllImport("kernel ...

  6. 读写INI文件操作类

    详情介绍:http://zh.wikipedia.org/wiki/INI%E6%96%87%E4%BB%B6 示例: 下面是一个虚拟的程序,其INI文件有两个小节,前面的小节是用来设置拥有者的信息, ...

  7. 【小丸类库系列】Excel操作类

    using Microsoft.Office.Interop.Excel; using System; using System.IO; using System.Reflection; namesp ...

  8. 【小丸类库系列】Word操作类

    using Microsoft.Office.Interop.Word; using System; using System.Collections.Generic; using System.Dr ...

  9. C++[类设计] ini配置文件读写类config

      //in Config.h #pragma once #include <windows.h> #include <shlwapi.h> #pragma comment(l ...

随机推荐

  1. eclipse不能打断点的问题

    今天突然eclipse不能打断点了,按ctrl+左键也不能进行方法导向了.查了很多资料还是不清楚怎么回事. 我把原来的文件再重新复制下,这个副本竟然是正常的. 结论:把原来的文件重新编译生成class ...

  2. html frames

    http://blog.sina.com.cn/s/blog_67697189010116o0.html *********************************************** ...

  3. 【转】java多态详解

    1.        Java中除了static和final方法外,其他所有的方法都是运行时绑定的.private方法都被隐式指定为final的,因此final的方法不会在运行时绑定.当在派生类中重写基 ...

  4. commonJS — 函数操作(for Function)

    for Function github: https://github.com/laixiangran/commonJS/blob/master/src/forFunction.js 代码 /** * ...

  5. requestAnimationFrame兼容性扩展

    /** * requestAnimationFrame兼容性扩展,两方面工作: * 1.把各浏览器前缀进行统一 * 2.在浏览器没有requestAnimationFrame方法时将其指向setTim ...

  6. kubernetes容器编排系统介绍

    版权声明:本文由turboxu原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/152 来源:腾云阁 https://www. ...

  7. 【转】 Linux chmod命令

    在Unix和Linux的各种操作系统下,每个文件(文件夹也被看作是文件)都按读.写.运行设定权限.例如我用ls -l命令列文件表时,得到如下输出:-rw-r--r-- 1 apple users 22 ...

  8. robotframework笔记20

    配置执行 选择测试用例 机器人框架提供了几个选择的命令行选项 执行测试用例. 也工作在相同的选项 后处理输出与 Rebot . 通过测试套件和测试用例的名称 测试套件和测试用例可以选择通过他们的名字的 ...

  9. Linux下修改系统时间并写入BIOS

    我们一般使用“date -s”命令来修改系统时间.比如将系统时间设定成2005年7月26日的命令如下. #date -s 07/26/2005 将系统时间设定成下午11点12分0秒的命令如下. #da ...

  10. CentOS中输入yum报错:sudo: unable to execute /bin/yum: No such file or directory

    今天尝试更新了下虚拟机CentOS中的python版本后. 运行“yum”命令,就报错:“sudo: unable to execute /bin/yum: No such file or direc ...