C#对*.ini文件的读写操作

本文介绍了一个使用C#实现的INI文件读写类。该类通过调用kernel32.dll中的API来完成对INI文件的操作,包括写入数据和读取数据的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1.using System;
2.using System.Collections.Generic;
3.using System.Runtime.InteropServices;
4.using System.Text;
5.6.namespace Ini
7.{
8.    /// <summary> 9.    /// 创建一个新的ini文件存储或者加载数据 10.    /// </summary> 11.    public class IniFile
12.    {
13.14.        public string path;
15.        [DllImport("kernel32")]
16.        private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
17.        [DllImport("kernel32")]
18.        private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
19.        /// <summary> 20.        /// ini文件构造器 21.        /// </summary> 22.        /// <param name="INIPath">输入参数为文件的路径</param> 23.        public IniFile(string INIPath)
24.        {
25.            path = INIPath;
26.        }
27.        /// <summary> 28.        /// 方法:向ini文件写入数据 29.        /// </summary> 30.        /// <param name="Section">段名:ini文件专有</param> 31.        /// <param name="Key">字段名</param> 32.        /// <param name="Value">字段值</param> 33.        public void IniWriteValue(string Section, string Key, string Value)
34.        {
35.            WritePrivateProfileString(Section, Key, Value, this.path);
36.        }
37.        /// <summary> 38.        /// 方法:从ini文件读取数据 39.        /// </summary> 40.        /// <param name="Section"></param> 41.        /// <param name="Key"></param> 42.        public void IniReadValue(string Section,string Key)
43.        {
44.            StringBuilder temp = new StringBuilder(255);
45.            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
46.            return temp.ToString();
47.        }
48.    }
49.}
50.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值