【C#】读取*.ini文件

一、前言

      在以前的项目中一般会把一下用户可以设置是值放到xml文件中,但是最近的项目中,项目组长说要用ini文件。ini文件小编也接触过,但是对他的读取还是有一定的挑战的。下面就对ini文件进行读取。

二、ini文件介绍

      通常C#使用基于XML的配置文件,不过如果有需要的话,比如要兼顾较老的系统,可能还是要用到INI文件。

      但C#本身并不具备读写INI文件的API,只有通过调用非托管代码的方式,即系统自身的API才能达到所需的目的。

格式

节(section)

节用方括号括起来,单独占一行,例如:
[section]

键(key)

键(key)又名属性(property),单独占一行用等号连接键名和键值,例如:
name=value

注释(comment)

注释使用英文分号(;)开头,单独占一行。在分号后面的文字,直到该行结尾都全部为注释,例如:
; comment text
[host]
# 设置服务器的ip- 王雷-2017年4月10日11:27:17
ip=222.168.33.117

[ports]
# 设置服务器端口-王雷-2017年4月10日11:27:31
port=8080

三、读取方法

      在读取之前呢,需要我们引入API:

using System;
using System.Runtime.InteropServices;
using System.Text;
namespace INIDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            WritePrivateProfileString("Demo", "abc", "123", "c:\\demo.ini");
            StringBuilder temp = new StringBuilder();
            GetPrivateProfileString("Demo", "abc", "", temp, 255, "c:\\demo.ini");
            Console.WriteLine(temp);
            Console.ReadLine();
        }
        [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
        private static extern bool WritePrivateProfileString(
            string lpAppName, string lpKeyName, string lpString, string lpFileName);
        [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
        private static extern int GetPrivateProfileString(
            string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString,
            int nSize, string lpFileName);
    }
}

      把方法封装,调用就可以:

   #region 读Ini文件

        public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                StringBuilder temp = new StringBuilder(1024);
                GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
                return temp.ToString();
            }
            else
            {
                return String.Empty;
            }
        }

        #endregion

        #region 写Ini文件

        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                bool OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        #endregion

四、小结

      通过这次的学习,对配置文件了解更加深刻了,读取中也有了深刻的认识。键值对的ini配置文件也是很不错的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

你个佬六

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

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

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

打赏作者

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

抵扣说明:

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

余额充值