开发接口程序时,对于接口程序配置的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. Objective-C与C++的区别

    1.两者的最大相同:都是从C演化而来的面相对象语言,两者都兼容标准C语言 2.两者的最大不同:Objective-C提供了运行期动态绑定机制,而C++是编译静态绑定,并且通过嵌入类(多重继承)和虚函数 ...

  2. golang json

    1.Go语言的JSON 库 Go语言自带的JSON转换库为 encoding/json 1.1)其中把对象转换为JSON的方法(函数)为 json.Marshal(),其函数原型如下 func Mar ...

  3. 扩展spring data jpa的数据更新方法时注意事项

    //此处必须加@Transactional,否则不能运行,报错 @Transactional @Modifying @Query("update ExamItem a set a.versi ...

  4. 【java】异常和处理

    (根据http://www.imooc.com/learn/110 陈码农老师教学视频总结)   一.异常体系结构 所有不正常类都继承于Throwable类 1.异常两个子类 error & ...

  5. 为什么删不掉date模块

    显示是field pending deletion一看report里面的field list并没有xxx_date_xxx,只好跑到数据库才看到一个field_date_test当时并没有把这个字段当 ...

  6. java 基础(第一天)

    1.  一个文件里面只能有一个 public 修饰的方法   且方法名与文件名保持一致. 如: public class main(){ } class car(){ } class dog(){ } ...

  7. Java 中String常用方法

    java中String的常用方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len ...

  8. ASP.NET Core文档中Work with Data章节的翻译目录

    作为初学者看了相关的教程,遇到的问题有: 1. 教程不是针对初学者,往往在某一方面教的较深,但并不系统,不适合初学者: 2. 虽然翻译的很顺畅,但是谈了自己较多的开发体会,初学者看着困难,尤其是TOM ...

  9. webstorm激活码

    2016.2.3版本 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZW ...

  10. 20145218 《Java程序设计》第02次实验报告

    北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1452 指导教师:娄嘉鹏 实验日期:2016.04.12 实验名称:Java面向对象程序设计 一.实验内容 初步掌握单元测试和T ...