开发接口程序时,对于接口程序配置的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. hibernate Criteria查询多对多(Set集合)条件筛选

    PO public class UserPO implements java.io.Serializable { /** 用户唯一标识ID */ private String id; /** 状态(在 ...

  2. java 嵌套类 简记

    嵌套类包括:1)静态嵌套类  (static 修饰符) 2)非静态嵌套类(又叫内部类) 其中内部类又可分为三种: 其一.在一个类(外部类)中直接定义的内部类: 其二.在一个方法(外部类的方法)中定义的 ...

  3. j2ee四大作用域pagecontext,request,session,ServletContext(转)

    转自:(http://www.5ycode.com/63) 在JSP页面中的对象,包括用户创建的对象(例如,JavaBean对象)和JSP的隐含对象,都有一个范围属性.范围定义了在什么时间内,在哪一个 ...

  4. SAP MM移动平均价和标准价逻辑

    从收货到领用,S一直都是以标准价格计算,V是实时更新 S 时将差异结转到在产品,产品中,最后结转到生产成本,最终到利润.具有计划性,可以控制考核 V 是实时更新,出现差异直接对应材料中调整.价格可以直 ...

  5. dede 调用四级导航

    一.修改文件:\include\taglib目录下的channel.lib.php,请将以下代码全部复制替换上述文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  6. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio »迁移

    Migrations¶ 4 of 4 people found this helpful The Contoso University sample web application demonstra ...

  7. golang作为server向android提供数据服务

    中间交换的数据是json ,后台数据库服务器是sqlserver2012 android通过post或者get方式访问 如get方式http://192.168.255.13:7080/tblFile ...

  8. 能源项目xml文件标签释义--DefaultAdvisorAutoProxyCreator

    [Spring]AOP拦截-三种方式实现自动代理 这里的自动代理,我讲的是自动代理bean对象,其实就是在xml中让我们不用配置代理工厂,也就是不用配置class为org.springframewor ...

  9. robotframework笔记27

    文档格式 可以使用简单的HTML格式 测试套件 , 测试用例 和 用户关键字 文档和 免费测试套件 元数据 在测试数据,以及当 记录测试 库 . 格式类似于大多数使用的风格 维基百科,它被设计成可以理 ...

  10. [Java] 获取本月周次和日期时间段信息

    package com.wdcloud.monitoring.common; import java.text.SimpleDateFormat; import java.util.ArrayList ...