C# 使用Hashtable处理Ini文件

这篇博客介绍了如何在C#中使用自定义的 IniFile 类来读写 Ini 文件。作者通过创建一个包含读取、写入、检查节和标识是否存在等方法的 IniFile 类,实现了对 Ini 文件的基本操作。示例代码展示了如何创建和保存 Ini 文件,以及如何从 Ini 文件读取特定值。

本人初学C#,未找到处理Ini文件的标准类,自己动手结合Hashtable写了一个。发布出来,与大家一起探讨,如果知道有其它更好的办法,麻烦通知我一下。

 

-------------------IniFile处理类-------------------

//IniFile read write funciton. author  zhulij@gmail.com ver 1.0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace common.io
{
    class IniFile
    {
        private Hashtable ht;
        private string iniFileName;

        public IniFile(String fileName)
        {
            iniFileName = fileName;
            ht = new Hashtable();
            FileInfo fi = new FileInfo(iniFileName);
            if (fi.Exists)
            {
                string line;
                string section = "", ident, val;
                StreamReader sr = new StreamReader(fileName);
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.StartsWith("[") & line.EndsWith("]"))
                    {
                        section = line.Substring(1, line.Length - 2);
                    }
                    else
                    {
                        ident = line.Substring(0, line.IndexOf('='));
                        val = line.Substring(line.IndexOf('=') + 1, line.Length - line.IndexOf('=') - 1);
                        if (ht.ContainsKey(section))
                        {
                            Hashtable valHt = (Hashtable)ht[section];
                            valHt.Add(ident, val);
                        }
                        else
                        {
                            Hashtable valHt = new Hashtable();
                            valHt.Add(ident, val);
                            ht.Add(section, valHt);
                        }
                    }
                }

                sr.Close();
            }
        }
      
        //Save To File
        public void Save()
        {
            FileInfo fi = new FileInfo(iniFileName);
            if (fi.Exists)
                fi.Delete();

            StreamWriter sw;
            sw = new StreamWriter(iniFileName);
            foreach (DictionaryEntry de in ht)
            {
                sw.WriteLine("[" + de.Key + "]");
                foreach (DictionaryEntry sde in ((Hashtable)de.Value))
                {
                    sw.WriteLine(sde.Key + "=" + sde.Value);
                }
            }
            sw.Close();
        }

        //ReadAStringVal
        public string ReadString(string section, string ident, string defaultVal)
        {
            string result = "";
            if (ht.ContainsKey(section))
            {
                Hashtable valHt = (Hashtable)ht[section];
                if (valHt.ContainsKey(ident))
                    result = valHt[ident].ToString();
            }

            return result;

        }

        //WriteAStringVal
        public void WriteString(string section, string ident, string val)
        {
            if (SectionExists(section))
            {
                Hashtable valHt = (Hashtable)ht[section];
                if (valHt.ContainsKey(ident))
                    valHt[ident] = val;
                else
                    valHt.Add(ident, val);
            }
            else
            {
                Hashtable valHt = new Hashtable();
                ht.Add(section, valHt);
                valHt.Add(ident, val);
            }
        }
       
        //SectionIsExists
        public bool SectionExists(string section)
        {
            if (ht.ContainsKey(section))
                return true;
            else
                return false;
        }
       
        //ValueIsExists
        public bool ValueExists(string section, string ident)
        {
            if (SectionExists(section) && ((Hashtable)ht[section]).ContainsKey(ident))
                return true;
            else
                return false;
        }

    }
}

 

-------------------调用示列-------------------

 

写INI文件

            IniFile iniFile = new IniFile("C://1.ini");
            iniFile.WriteString("A","A1","1");
            iniFile.WriteString("A", "A2", "2");
            iniFile.WriteString("A", "A3", "3");
            iniFile.WriteString("B", "B1", "11");
            iniFile.WriteString("B", "B2", "12");
            iniFile.WriteString("A", "A4", "4");
            iniFile.Save();

 

读INI文件

           IniFile iniFile = new IniFile("C://1.ini");
           textBox3.Text=  iniFile.ReadString("A","A2""");

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值