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""");
- C# 使用Hashtable处理Ini文件
- c# INI文件处理
- 使用iniparser 处理INI文件
- 使用C#操作ini文件
- 使用C#操作ini文件
- 使用C#操作INI文件
- 使用C#操作ini文件
- C# PC端读取INI文件处理
- C#中处理读写Ini文件
- [C#]使用C#操作INI文件
- Wince 下C#使用INI文件
- 处理INI文件
- INI 文件的处理
- C#读写INI文件
- C#读写INI文件
- C#读写INI文件
- C#读写INI文件
- c#读取ini文件
- FreeBSD 配置路由
- 惰性求值与尾递归的关系2
- IIS无法显示ASP网页解决方案
- 金山卫士界面源码解读及界面库分离(附录) --你问我答
- PHP程序设计Zend Framework(ZF)Invalid controller specified真正解决办法
- C# 使用Hashtable处理Ini文件
- 注册机原理分析【未完】
- 我对C++的前景看法 C++和C#的对比
- 《编程之美》读书笔记集锦
- 摩斯密码表
- 换了新工作,开一个博客记录一下
- memstr
- ArrayList和LinkedList
- java各种排序