C# winform 创建,修改,删除 ini配置文件

本文介绍了一个用于处理INI文件的C#类,该类提供了一系列方法来读取、写入、删除INI文件中的条目,并支持读取整数、布尔值等不同数据类型。

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

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

 

namespace EmailCustomer

{

    class IniFile

    {

        private string path;

 

        ///   <summary> 

        ///   实例初始化为指定路径的INI文件。 

        ///   </summary> 

        ///   <param   name= "path "> INI文件路径。 </param> 

        public IniFile(string path)

        {

            this.path = path;

        }

 

        ///   <summary> 

        ///   获取INI文件的路径。 

        ///   </summary> 

        public string Path

        {

            get

            {

                return path;

            }

        }

 

        ///   <summary> 

        ///   读取指定小节下指定条目的字符串。 

        ///   </summary> 

        ///   <param   name= "sectionName "> 欲在其中查找条目的小节名称。这个字串不区分大小写。 </param> 

        ///   <param   name= "keyName "> 欲获取的项名或条目名。这个字串不区分大小写。 </param> 

        ///   <param   name= "defaultValue "> 指定的条目没有找到时返回的默认值。 </param> 

        ///   <returns> 指定小节下指定条目的字符串。 </returns> 

        ///   <remarks> 如果sectionName为null,则返回所有小节的列表,如果keyName为null,指定小节所有项的列表。 </remarks> 

        public string ReadString(string sectionName, string keyName, string defaultValue)

        {

            const int MAXSIZE = 255;

            StringBuilder temp = new StringBuilder(MAXSIZE);

            GetPrivateProfileString(sectionName, keyName, defaultValue, temp, 255, this.path);

            return temp.ToString();

        }

 

        public void WriteString(string sectionName, string keyName, string value)

        {

            WritePrivateProfileString(sectionName, keyName, value, this.path);

        }

 

        public int ReadInteger(string sectionName, string keyName, int defaultValue)

        {

            return GetPrivateProfileInt(sectionName, keyName, defaultValue, this.path);

        }

 

        public void WriteInteger(string sectionName, string keyName, int value)

        {

            WritePrivateProfileString(sectionName, keyName, value.ToString(), this.path);

        }

 

        public bool ReadBoolean(string sectionName, string keyName, bool defaultValue)

        {

            int temp = defaultValue ? 1 : 0;

            int result = GetPrivateProfileInt(sectionName, keyName, temp, this.path);

            return (result == 0 ? false : true);

        }

 

        public void WriteBoolean(string sectionName, string keyName, bool value)

        {

            string temp = value ? "1 " : "0 ";

            WritePrivateProfileString(sectionName, keyName, temp, this.path);

        }

 

        ///   <summary> 

        ///   删除这个项现有的字串。 

        ///   </summary> 

        ///   <param   name= "sectionName "> 要设置的项名或条目名。这个字串不区分大小写。 </param> 

        ///   <param   name= "keyName "> 要删除的项名或条目名。这个字串不区分大小写。 </param> 

        public void DeleteKey(string sectionName, string keyName)

        {

            WritePrivateProfileString(sectionName, keyName, null, this.path);

        }

 

        ///   <summary> 

        ///   删除这个小节的所有设置项。 

        ///   </summary> 

        ///   <param   name= "sectionName "> 要删除的小节名。这个字串不区分大小写。 </param> 

        public void EraseSection(string sectionName)

        {

            WritePrivateProfileString(sectionName, null, null, this.path);

        }

 

        [DllImport("kernel32")]

        public static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);

        [DllImport("kernel32")]

        public static extern int GetPrivateProfileInt(string lpApplicationName, string lpKeyName, int nDefault, string lpFileName);

        [DllImport("kernel32")]

        public static extern int GetPrivateProfileString(string lpApplicationName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值