温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#操作ini文件的帮助类是什么

发布时间:2022-04-24 16:14:53 来源:亿速云 阅读:111 作者:iii 栏目:开发技术

这篇文章主要介绍了C#操作ini文件的帮助类是什么的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C#操作ini文件的帮助类是什么文章都会有所收获,下面我们一起来看看吧。

一、定义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文件的帮助类是什么”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ini
AI