c#读取ini文件中内容_在C#中读取和解析INI文件

本文介绍如何使用C#读取和解析INI文件。通过System.Collections.Hashtable类,可以方便地枚举和访问文件中的所有设置,适用于需要配置文件的应用程序。
摘要由CSDN通过智能技术生成

c#读取ini文件中内容

此类利用System.Collections.Hashtable枚举INI文件中的所有设置,以便于访问。 它非常简单,并且完全可重用。 对于需要设置文件的任何应用程序来说都是可靠的补充。

using System;
using System.IO;
using System.Collections; 
public class IniParser
{
     
    private Hashtable keyPairs = new Hashtable();
    private String iniFilePath; 
    private struct SectionPair
    {
     
        public String Section;
        public String Key;
    } 
    /// <summary>
    /// Opens the INI file at the given path and enumerates the values in the IniParser.
    /// </summary>
    /// <param name="iniPath">Full path to INI file.</param>
    public IniParser(String iniPath)
    {
     
        TextReader iniFile = null;
        String strLine = null;
        String currentRoot = null;
        String[] keyPair = null; 
        iniFilePath = iniPath; 
        if (File.Exists(iniPath))
        {
     
            try
            {
     
                iniFile = new StreamReader(iniPath); 
                strLine = iniFile.ReadLine(); 
                while (strLine != null)
                {
     
                    strLine = strLine.Trim().ToUpper(); 
                    if (strLine != "")
                    {
     
                        if (strLine.StartsWith("[") && strLine.EndsWith("]"))
                        {
     
                            currentRoot = strLine.Substring(1, strLine.Length - 2);
                        }
                        else
       
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值