C# 读取ini文件、键值对操作

.Net实践-C#如何读取、写入INI配置文件

C# 读取ini文件

简述

最近接触到INI配置文件的读写,虽然很久以前微软就推荐使用注册表来代替INI配置文件,现在在Visual Studio上也有专门的.Net配置文件格式,但是看来看去还是INI配置文件顺眼。事实上.Net的XML格式配置文件在功能上更加强大,我也更推荐 大家使用这种类型的配置文件来进行.Net软件的开发,我之所以使用INI配置文件,无非是想尝一下鲜和个人习惯而已。

C#本身没有提供访问INI配置文件的方法,但是我们可以使用WinAPI提供的方法来处理INI文件的读写,代码很简单!网上有大量现成的代码,这里只是作为记录和整理,方便日后使用。

INI配置文件组成

INI文件是文本文件,由若干节(section)组成,在每个带中括号的节名称下,是若干个关键词(key)及其对应的值(Value),这些关键词(key)属于位于关键词(key)上的节(section)。

[Section]
Key1=Value1
Key2=Value2

核心代码

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
 
namespace ToolsLibrary
{
    public class IniFile
    {
        public string path;             //INI文件名 
         
        //声明写INI文件的API函数
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
         
        //声明读INI文件的API函数
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
 
        //类的构造函数,传递INI文件的路径和文件名
        public IniFile(string INIPath) 
        { 
            path = INIPath; 
        } 
 
        //写INI文件
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, path);
        }
 
        //读取INI文件
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
            return temp.ToString();
        }
      
    } 
}

使用方法

在以后使用的时候,我们只要实例化一个IniFile对象,即可通过这个对象中的方法读写INI配置文件。

读取INI配置文件中的值

IniFile ini = new IniFile("C://config.ini"); 
BucketName = ini.IniReadValue("operatorinformation","bucket");
OperatorName = ini.IniReadValue("operatorinformation", "operatorname");
OperatorPwd = ini.IniReadValue("operatorinformation", "operatorpwd");

将值写入INI配置文件中

IniFile ini = new IniFile("C://config.ini"); 
ini.IniWriteValue("operatorinformation", "bucket", BucketName);
ini.IniWriteValue("operatorinformation", "operatorname", OperatorName);
ini.IniWriteValue("operatorinformation", "operatorpwd", OperatorPwd);

C# 键值对操作

using System;
using System.Collections.Generic;
namespace _09键值对
{
    class Program
    {
        static void Main(string[] args)
        {
            //Dictionary
            //定义一个键值对集合
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            //添加键值对数据,键必须唯一,值可重复
            dictionary.Add("1", "张珊");
            dictionary.Add("2", "李四");
            dictionary.Add("3", "王五");
            dictionary.Add("4", "王八");
            //重赋值
            dictionary["3"] = "沈继涵";
 
 
 
            //判断集合中是否含有某一个键ContainsKey()
            if (!dictionary.ContainsKey("5"))
            {
                dictionary.Add("5", "杨过");//不含则加
            }
            else
            {
                dictionary["5"] = "杨过";//含则改
            }
 
            Console.WriteLine(dictionary["5"]);
 
            //用foreach
            //通过键遍历集合
            foreach (string item in dictionary.Keys)
            {
 
                Console.WriteLine("键--{0}   值--{1}", item, dictionary[item]);
            }
 
            //通过键值对遍历集合
            foreach (KeyValuePair<string, string> kv in dictionary)
            {
                Console.WriteLine("键--{0}   值--{1}", kv.Key, kv.Value);
            }
 
            Console.ReadKey();
        }
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CDamogu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值