C#操作读写INI配置文件
一个完整的INI文件格式由节(section)、键(key)、值(value)组成。
示例如:
[section]
key1=value1
key2=value2
; 备注:value的值不要太长,理论上最多不能超过65535个字节。
在Windows程序开发中经常会遇到读写INI配置文件的情况,以下C#类封装了对INI配置文件读写修改的操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace CRApp
{
public class INIHelper
{
private string FilePath;
public string Path
{
get { return this.FilePath; }
set
{
if (value.Substring(0, 1) == "\\" || value.Substring(0, 1) == "/")
{
this.FilePath = AppDomain.CurrentDomain + value;
}
else
{
this.FilePath = value;
}
}
}
[DllImport("kernel32")]
private static extern bool 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);
public INIHelper(string _Path)
{
this.Path = _Path;
}
public void WriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.FilePath);
}
public string ReadValue(string Section, string Key)
{
try
{
StringBuilder temp = new StringBuilder();
int i = GetPrivateProfileString(Section, Key, "", temp, -1, this.FilePath);
return temp.ToString();
}
catch { return ""; }
}
public void RemoveKey(string Section, string Key)
{
WritePrivateProfileString(Section, Key, null, this.FilePath);
}
public bool RemoveSection(string Section)
{
return WritePrivateProfileString(Section, null, null, this.FilePath);
}
public bool Exists()
{
return System.IO.File.Exists(this.FilePath);
}
}
}
参考:
[1] WritePrivateProfileString 函数: https://baike.baidu.com/item/WritePrivateProfileString/9711454
[2] GetPrivateProfileString 函数: https://baike.baidu.com/item/GetPrivateProfileString/9642262
C#操作读写INI配置文件的更多相关文章
- C# 读写 ini 配置文件
虽说 XML 文件越发流行,但精简的 ini 配置文件还是经常会用到,在此留个脚印. 当然,文中只是调用系统API,不会报错,如有必要,也可以直接以流形式读取 ini文件并解析. /// <su ...
- 引用“kernel32”读写ini配置文件
引用"kernel32"读写ini配置文件 unity ini kernel32 配置文件 引用"kernel32"读写ini配置文件 OverView ke ...
- C# 文件的一些基本操作(转)//用C#读写ini配置文件
C# 文件的一些基本操作 2009-07-19 来自:博客园 字体大小:[大 中 小] 摘要:介绍C#对文件的一些基本操作,读写等. using System;using System.IO;us ...
- [转]VB 读写ini 配置文件
转自 百度知道 C# 读写 ini配置文件 点此链接 'API 声明Public Declare Function GetPrivateProfileString Lib "kernel32 ...
- 自己写的 读写 ini 配置文件类
/// <summary> /// 不调用系统API 读写 ini 配置文件 /// </summary> public class RW_ini { #region ==== ...
- C#+Access 员工信息管理--简单的增删改查操作和.ini配置文件的读写操作。
1.本程序的使用的语言是C#,数据库是Access2003.主要是对员工信息进行简单的增删改查操作和对.ini配置文件的读写操作. 2.代码运行效果如下: 功能比较简单.其中在得到查询结果后,在查询结 ...
- c#读写ini配置文件示例
虽然c#里都是添加app.config 并且访问也很方便 ,有时候还是不习惯用他.那么我们来做个仿C++下的那种ini配置文件读写吧 其他人写的都是调用非托管kernel32.dll.我也用过 ...
- C++读写ini配置文件GetPrivateProfileString()&WritePrivateProfileString()
转载: 1.https://blog.csdn.net/fengbingchun/article/details/6075716 2. 转自:http://hi.baidu.com/andywangc ...
- 使用C#读写ini配置文件
INI就是扩展名为"INI"的文件,其实他本身是个文本文件,可以用记事本打工,主要存放的是用户所做的选择或系统的各种参数. INI文件其实并不是普通的文本文件.它有自己的结构.由若 ...
随机推荐
- react中antd+css Module一起使用
antd 和 css modules 不能混用,针对antd的css 单独写一条loader的规则,不开启 css modules. 使用 exclude 和 include 配置参考(https:/ ...
- centos7 alias别名永久生效
进入/etc/profile.d/目录 cd /etc/profile.d/ 在profile.d目录随意创建一个sh文件,例如alias_test.sh vi alias_test.sh##里面的内 ...
- Python常用功能函数系列总结(四)之数据库操作
本节目录 常用函数一:redis操作 常用函数二:mongodb操作 常用函数三:数据库连接池操作 常用函数四:pandas连接数据库 常用函数五:异步连接数据库 常用函数一:redis操作 # -* ...
- 信息收集&Fuzz
本文译自https://0xjoyghosh.medium.com/information-gathering-scanning-for-sensitive-information-reloaded- ...
- Mybatis 学习记录 续
项目结构如下: 1.数据库建表 表名:user 结构: 内容: 2.pom.xml文件更新如下: 注:其中build部分尤其需要重视 <?xml version="1.0" ...
- linux下编译支持opencl的opencv for android
主要的步骤其他人已经写过,请参考这篇:https://www.cnblogs.com/hrlnw/p/4720977.html 操作的细节请参考附件的pdf: https://files.cnblo ...
- window10教育版激活失败
问题 输入完key之后显示无法连接服务器 再次输入密钥无效,而且家庭版密钥激活也没了 使用命令行消除过去的key,使用新的教育版key后,显示运行在运行microsoft windows 非核心版本的 ...
- 字符串工具类ToStringBuilder常用方法介绍
一.简介与引入 1.ToStringBuilder.HashCodeBuilder.EqualsBuilder.ToStringStyle.ReflectionToStringBuilder.Co ...
- AOP-底层原理
AOP(底层原理) 1,AOP底层使用动态代理 (1)有两种情况动态代理 第一种 有接口情况,使用JDK动态代理 *创建接口实现类代理对象,增强类的方法 第二种 无接口情况,使用CGLIB动态代理 * ...
- 如何修改主机名hostname
hostname是Linux系统下的一个内核参数,它保存在/proc/sys/kernel/hostname下,但是它的值是Linux启动时从rc.sysinit读取的.而/etc/rc.d/rc.s ...