C# 使用Hashtable处理Ini文件

来源:互联网 发布:小红书 知乎 编辑:程序博客网 时间:2024/06/05 12:42

本人初学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""");

原创粉丝点击