C#操作ini文件的帮助类
作者:農碼一生 时间:2022-12-26 17:11:09
.ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置,一般用户就用windows提供的各项图形化管理界面就可实现相同的配置了。但在某些情况,还是要直接编辑ini才方便,一般只有很熟悉windows才能去直接编辑。
一、定义Class
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace IniDemo
{
public class IniFile
{
private string m_FileName;
public string FileName
{
get
{
return this.m_FileName;
}
set
{
this.m_FileName = value;
}
}
[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);
[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
[DllImport("kernel32.dll")]
private static extern int WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
public IniFile(string aFileName)
{
this.m_FileName = aFileName;
}
public IniFile()
{
}
public int ReadInt(string section, string name, int def)
{
return IniFile.GetPrivateProfileInt(section, name, def, this.m_FileName);
}
public string ReadString(string section, string name, string def)
{
StringBuilder stringBuilder = new StringBuilder(2048);
IniFile.GetPrivateProfileString(section, name, def, stringBuilder, 2048, this.m_FileName);
return stringBuilder.ToString();
}
public void WriteInt(string section, string name, int Ival)
{
IniFile.WritePrivateProfileString(section, name, Ival.ToString(), this.m_FileName);
}
public void WriteString(string section, string name, string strVal)
{
IniFile.WritePrivateProfileString(section, name, strVal, this.m_FileName);
}
public void DeleteSection(string section)
{
IniFile.WritePrivateProfileString(section, null, null, this.m_FileName);
}
public void DeleteAllSection()
{
IniFile.WritePrivateProfileString(null, null, null, this.m_FileName);
}
public string IniReadValue(string section, string name)
{
StringBuilder stringBuilder = new StringBuilder(256);
IniFile.GetPrivateProfileString(section, name, "", stringBuilder, 256, this.m_FileName);
return stringBuilder.ToString();
}
public void IniWriteValue(string section, string name, string value)
{
IniFile.WritePrivateProfileString(section, name, value, this.m_FileName);
}
}
}
二、调用方法
IniFile iniFile = new IniFile(Environment.CurrentDirectory + "\\LocalInf.ini");
//读取Local节点下M的值,默认为空值
string m = iniFile.ReadString("Local", "M", "");
//Local节点下写F=f
iniFile.WriteString("Local", "F", "f");
//读取Local节点下IsSleep的字符串值,并转为bool类型值,给出默认值为False
bool f = bool.Parse(iniFile.ReadString("Local", "IsSleep", "False"));
//读取Local节点下的C的字符串值,并转为double类型值,给出默认值0
bool f = double.Parse(iniFile.ReadString("Local", "C", "0"));
来源:https://www.cnblogs.com/wml-it/p/15618356.html
标签:C#,操作,ini,文件,帮助,类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android实现背景可滑动登录界面 (不压缩背景弹出键盘)
2023-11-14 11:09:07
![](https://img.aspxhome.com/file/2023/9/138469_0s.gif)
Mybatis-Plus 多表联查分页的实现代码
2023-11-25 04:59:07
SpringBoot如何获取Kafka的Topic列表
2023-11-26 16:01:52
![](https://img.aspxhome.com/file/2023/2/60242_0s.png)
Android Studio配置(Android Studio4.1为例)
2022-04-18 04:19:18
![](https://img.aspxhome.com/file/2023/7/137597_0s.png)
java分割文本字符串的方法
2023-11-13 03:52:24
VC实现屏幕截词功能的方法详解
2022-08-05 07:22:18
C语言枚举(enum)和联合(union)实例分享
2023-06-17 01:56:42
SpringBoot整合Pulsar的实现示例
2021-10-09 17:39:35
ShardingSphere解析SQL示例详解
2023-11-23 13:57:55
解析Java的Hibernate框架中的持久化类和映射文件
2023-08-19 15:00:05
java将一个目录下的所有数据复制到另一个目录下
2023-01-08 15:11:44
C#编程实现四舍五入、向上及下取整的方法
2023-10-25 07:59:38
Java逃逸分析详解及代码示例
2021-05-29 15:23:15
关于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服务传输的问题
2023-01-04 15:38:04
![](https://img.aspxhome.com/file/2023/0/110790_0s.jpg)
java 引用传递的三种类型小结
2023-09-03 03:59:47
![](https://img.aspxhome.com/file/2023/0/60560_0s.png)
C#实现合并多个word文档的方法
2022-10-02 08:32:39
Input系统之InputReader处理按键事件详解
2023-11-09 20:57:05
SpringBoot 上传文件判空以及格式检验流程
2023-01-19 05:07:36
![](https://img.aspxhome.com/file/2023/5/61405_0s.png)
C#中if语句使用概述
2022-08-31 23:22:09
SpringBoot面试突击之过滤器和拦截器区别详解
2022-10-13 02:02:05
![](https://img.aspxhome.com/file/2023/8/84638_0s.png)