这篇文章介绍了C#操作ini文件的帮助类,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
.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"));
到此这篇关于C#操作ini文件帮助类的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持得得之家。
沃梦达教程
本文标题为:C#操作ini文件的帮助类
基础教程推荐
猜你喜欢
- C#类和结构详解 2023-05-30
- C#控制台实现飞行棋小游戏 2023-04-22
- C# 调用WebService的方法 2023-03-09
- ZooKeeper的安装及部署教程 2023-01-22
- C# windows语音识别与朗读实例 2023-04-27
- winform把Office转成PDF文件 2023-06-14
- C# List实现行转列的通用方案 2022-11-02
- unity实现动态排行榜 2023-04-27
- 一个读写csv文件的C#类 2022-11-06
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26