开发接口程序时,对于接口程序配置的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. linux 文件类型 文件权限

    linux中常见的文件类型有: “—”表示普通文件 :-rw-r--r-- 1 root root 41727 07-13 02:56 install.log   “d”表示目录 :drwxr-xr- ...

  2. hibernate.properties和hibernate.cfg.xml

    hibernate.properties和hibernate.cfg.xml 博客分类: 框架技术 HibernateXMLSQLOracleJDBC     hibernate配置文件可以有两种方式 ...

  3. Bootstrap的标题

    一.定义标题 Bootstrap和普通的HTML页面一样,定义标题都是使用标签<h1>到<h6>,只不过Bootstrap覆盖了其默认的样式,使用其在所有浏览器下显示的效果一样 ...

  4. Android控件之CheckBox(复选框控件)

    一.有两种状态: 选中状态(true).未选中状态(false) 二.属性 android:id = "@+id/checkbox" android:layout_width=&q ...

  5. 工作流学习——Activiti流程定义管理三步曲 (zhuan)

    http://blog.csdn.net/zwk626542417/article/details/46602419 ***************************************** ...

  6. hiho_1054_滑动解锁

    题目大意 智能手机九点屏幕滑动解锁,如果给出某些连接线段,求出经过所有给出线段的合法的滑动解锁手势的总数.题目链接: 滑动解锁 题目分析 首先,尝试求解没有给定线段情况下,所有合法的路径的总数.可以使 ...

  7. HBase之过滤器

    filter ==> SQL 中的Where filter的执行流程: 过滤器在客户端创建,然后通过RPC发送到服务器上,由服务器执行   基础过滤器:   比较器: Comparator  D ...

  8. 有关mipmaps

    Mipmaps的作用是什么,仅仅是为了使屏幕三角形和纹理三角形的差异变小?有没有以空间换时间的理念? Mipmaps在生成一系列小的纹理样本时, 是如何从原始纹理采样的?即如何生成这些小的纹理样本.

  9. java中身份证号15位转18位

    /** * 将15位转换为18位 * @param idCode 15位身份证号 * @return String 18位身份证号 */ public String toEighteen(String ...

  10. jmeter 构建一个Web测试计划

    添加用户 第一步你想做的每一个JMeter测试计划是添加一个 线程组 元素. 线程组告诉 JMeter的用户数量你想模拟,用户应该发送的次数 请求,请求他们应该送多少. 继续添加ThreadGroup ...