【个人使用.Net类库】(1)INI配置文件操作类
开发接口程序时,对于接口程序配置的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配置文件操作类的更多相关文章
- C# ini配置文件操作类
/// <summary> /// INI文件操作类 /// </summary> public class IniFileHelper { /// <summary&g ...
- Ini文件操作类
/// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...
- C# 配置文件操作类
注意添加引用:System.Configuration: using System; using System.Collections.Generic; using System.Text; usin ...
- Ini配置文件操作
package cn.com.szhtkj.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOE ...
- C# INI配置文件读写类
ini是一种很古老的配置文件,C#操作ini文件借助windows底层ini操作函数,使用起来很方便: public class IniHelper { [DllImport("kernel ...
- 读写INI文件操作类
详情介绍:http://zh.wikipedia.org/wiki/INI%E6%96%87%E4%BB%B6 示例: 下面是一个虚拟的程序,其INI文件有两个小节,前面的小节是用来设置拥有者的信息, ...
- 【小丸类库系列】Excel操作类
using Microsoft.Office.Interop.Excel; using System; using System.IO; using System.Reflection; namesp ...
- 【小丸类库系列】Word操作类
using Microsoft.Office.Interop.Word; using System; using System.Collections.Generic; using System.Dr ...
- C++[类设计] ini配置文件读写类config
//in Config.h #pragma once #include <windows.h> #include <shlwapi.h> #pragma comment(l ...
随机推荐
- 使用js加载器动态加载外部Javascript文件
原文:http://www.cnblogs.com/xdp-gacl/p/3927417.html 今天在网上找到了一个可以动态加载js文件的js加载器,具体代码如下: JsLoader.js var ...
- Tomcat:IOException while loading persisted sessions: java.io.EOFException解决手记
原文:http://blog.csdn.net/lifuxiangcaohui/article/details/37659905 一直用tomcat一段时间都正常无事,最近一次启动tomcat就发生以 ...
- SQL 比较时间大小
比较字符串类型的时间大小 数据库中的时间是varchar类型的,MySql使用CURDATE()来获取当前日期,SqlServer通过GETDATE()来获取当前日期 1. 直接使用字符串来比较 注意 ...
- LinuxShell脚本攻略--第一章 小试牛刀
使用 shell 进行数学运算: #!/bin/bash no1=; no2=; let result=no1+no2 echo $result result=$[ $no1 + no2 ] resu ...
- win7下安装MYSQL报错:"MYSQL 服务无法启动"的3534问题
上午在win7下安装MYSQL,只到“net start mysql”这一步报错:3534的错误: 于是在百度中搜索关键字“mysql服务无法启动3534”. 参考以下两个链接中的方法,解决了3534 ...
- VBA对象模型(1)
关于对象和集合的比喻 Excel的基本单元是Workbook对象:在快餐连锁店中,基本的单元是单个餐馆.使用Excel可以添加工作簿和关闭工作簿,所有打开的工作簿组成了Workbooks集合(Work ...
- 数据库中Schema(模式)概念的理解
在学习SQL的过程中,会遇到一个让你迷糊的Schema的概念.实际上,schema就是数据库对象的集合,这个集合包含了各种对象如:表.视图.存储过程.索引等.为了区分不同的集合,就需要给不同的集合起不 ...
- python中join的用法
str.join(sequence) # 将序列中的元素以str字符连接生成一个新的字符串 list1 = ['a', 'b', 'c'] new_str = '-'.join(list1) # 输出 ...
- hdu---(3555)Bomb(数位dp(入门))
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submi ...
- HDU-----(4858)项目管理(模拟)
项目管理 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...