高效的Ini文件访问类(C#编写,基于Compact Framework 2.0),在网上也搜索到几种C#的算法,只是感觉效率太低,就改进了一下算法,只完成ini文件操作的基本功能,效率提高10-30倍。但是本方法没有考虑到其他的一些ini文件的特性(比如文件内可能有空行),故特殊情况下可能要修改代码。
高效的Ini文件访问类(C#编写,基于Compact Framework 2.0),在网上也搜索到几种C#的算法,只是感觉效率太低,就改进了一下算法,只完成ini文件操作的基本功能,效率提高10-30倍。但是本方法没有考虑到其他的一些ini文件的特性(比如文件内可能有空行),故特殊情况下可能要修改代码。
类文件内容如下:
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
public class IniCF
{
/**//// <summary>
/// Copyright (C) 2008 lgamoy@163.com
/// Ini文件访问类(基于Compact Framework 2.0)
/// 说明:该类是在Window Mobile上使用的,所以为了保证执行高效,丢失了一些ini文件的特性。比如文件中不能有空行,否则读取不到。
// Fields
public string path = string.Empty;
// Methods
public IniCF(string iniFilePath)
{
this.path = iniFilePath;
}
读取#region 读取
/**//// <summary>
/// 读取
/// </summary>
/// <param name="ApplicationName">节名</param>
/// <param name="KeyName">键名</param>
/// <param name="Default">默认值</param>
/// <param name="FileName">ini文件名</param>
/// <returns></returns>
private string GetPrivateProfileString(string ApplicationName, string KeyName, string Default, string FileName)
{
string str = string.Format("[{0}]", ApplicationName);
string str2 = KeyName + "=";
StreamReader reader = null;
try
{
string str3 = string.Empty;
bool flag = false;
reader = new StreamReader(FileName, Encoding.Default);
str3 = reader.ReadLine();
for (str3 = (str3 == null) ? string.Empty : str3.Trim(); str3.Length > 0; str3 = (str3 == null) ? string.Empty : str3.Trim())
{
if (!flag)
{
if (str3 == str)
{
flag = true;
}
}
else
{
if (str3.StartsWith("[") && str3.EndsWith("]"))
{
return Default;
}
if (str3.StartsWith(str2))
{
return str3.Substring(KeyName.Length + 1);
}
}
str3 = reader.ReadLine();
}
}
catch
{
}
finally
{
if (reader != null)
{
reader.Close();
}
}
return Default;
}
#endregion
写入#region 写入
/**//// <summary>
/// ini文件写入
/// </summary>
/// <param name="ApplicationName">节名</param>
/// <param name="KeyName">键名</param>
/// <param name="Value">要写入的值</param>
/// <param name="FileName">ini文件</param>
private void WritePrivateProfileString(string ApplicationName, string KeyName, string Value, string FileName)
{
string str = string.Format("[{0}]", ApplicationName);
string str2 = KeyName + "=";
try
{
StringBuilder builder = new StringBuilder();
bool flag = false;
bool flag2 = false;
bool flag3 = true;
StreamReader reader = new StreamReader(FileName, Encoding.Default);
string str3 = reader.ReadLine();
for (str3 = (str3 == null) ? string.Empty : str3.Trim(); str3.Length > 0; str3 = (str3 == null) ? string.Empty : str3.Trim())
{
if (flag3)
{
if (!flag2)
{
builder.Append((builder.Length == 0) ? string.Empty : "\r\n");
builder.Append(str3);
if (str3 == str)
{
flag2 = true;
}
}
else if (str3.StartsWith("[") && str3.EndsWith("]"))
{
builder.Append(string.Format("\r\n{0}={1}", KeyName, Value));
builder.Append(string.Format("\r\n{0}", str3));
flag = true;
flag3 = false;
}
else if (str3.StartsWith(str2))
{
builder.Append(string.Format("\r\n{0}={1}", KeyName, Value));
flag = true;
flag3 = false;
}
else
{
builder.Append(string.Format("\r\n{0}", str3));
}
}
else
{
builder.Append(string.Format("\r\n{0}", str3));
}
str3 = reader.ReadLine();
}
reader.Close();
if (!flag2)
{
builder.Append((builder.Length == 0) ? string.Empty : "\r\n");
builder.Append(string.Format("[{0}]", ApplicationName));
}
if (!flag)
{
builder.Append(string.Format("\r\n{0}={1}", KeyName, Value));
}
StreamWriter writer = new StreamWriter(FileName, false, Encoding.Default);
writer.Write(builder);
writer.Close();
}
catch
{
}
}
#endregion
public string IniReadValue(string Section, string Key)
{
return this.GetPrivateProfileString(Section, Key, string.Empty, this.path);
}
public void IniWriteValue(string Section, string Key, string Value)
{
this.WritePrivateProfileString(Section, Key, Value, this.path);
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
public class IniCF
{
/**//// <summary>
/// Copyright (C) 2008 lgamoy@163.com
/// Ini文件访问类(基于Compact Framework 2.0)
/// 说明:该类是在Window Mobile上使用的,所以为了保证执行高效,丢失了一些ini文件的特性。比如文件中不能有空行,否则读取不到。
// Fields
public string path = string.Empty;
// Methods
public IniCF(string iniFilePath)
{
this.path = iniFilePath;
}
读取#region 读取
/**//// <summary>
/// 读取
/// </summary>
/// <param name="ApplicationName">节名</param>
/// <param name="KeyName">键名</param>
/// <param name="Default">默认值</param>
/// <param name="FileName">ini文件名</param>
/// <returns></returns>
private string GetPrivateProfileString(string ApplicationName, string KeyName, string Default, string FileName)
{
string str = string.Format("[{0}]", ApplicationName);
string str2 = KeyName + "=";
StreamReader reader = null;
try
{
string str3 = string.Empty;
bool flag = false;
reader = new StreamReader(FileName, Encoding.Default);
str3 = reader.ReadLine();
for (str3 = (str3 == null) ? string.Empty : str3.Trim(); str3.Length > 0; str3 = (str3 == null) ? string.Empty : str3.Trim())
{
if (!flag)
{
if (str3 == str)
{
flag = true;
}
}
else
{
if (str3.StartsWith("[") && str3.EndsWith("]"))
{
return Default;
}
if (str3.StartsWith(str2))
{
return str3.Substring(KeyName.Length + 1);
}
}
str3 = reader.ReadLine();
}
}
catch
{
}
finally
{
if (reader != null)
{
reader.Close();
}
}
return Default;
}
#endregion
写入#region 写入
/**//// <summary>
/// ini文件写入
/// </summary>
/// <param name="ApplicationName">节名</param>
/// <param name="KeyName">键名</param>
/// <param name="Value">要写入的值</param>
/// <param name="FileName">ini文件</param>
private void WritePrivateProfileString(string ApplicationName, string KeyName, string Value, string FileName)
{
string str = string.Format("[{0}]", ApplicationName);
string str2 = KeyName + "=";
try
{
StringBuilder builder = new StringBuilder();
bool flag = false;
bool flag2 = false;
bool flag3 = true;
StreamReader reader = new StreamReader(FileName, Encoding.Default);
string str3 = reader.ReadLine();
for (str3 = (str3 == null) ? string.Empty : str3.Trim(); str3.Length > 0; str3 = (str3 == null) ? string.Empty : str3.Trim())
{
if (flag3)
{
if (!flag2)
{
builder.Append((builder.Length == 0) ? string.Empty : "\r\n");
builder.Append(str3);
if (str3 == str)
{
flag2 = true;
}
}
else if (str3.StartsWith("[") && str3.EndsWith("]"))
{
builder.Append(string.Format("\r\n{0}={1}", KeyName, Value));
builder.Append(string.Format("\r\n{0}", str3));
flag = true;
flag3 = false;
}
else if (str3.StartsWith(str2))
{
builder.Append(string.Format("\r\n{0}={1}", KeyName, Value));
flag = true;
flag3 = false;
}
else
{
builder.Append(string.Format("\r\n{0}", str3));
}
}
else
{
builder.Append(string.Format("\r\n{0}", str3));
}
str3 = reader.ReadLine();
}
reader.Close();
if (!flag2)
{
builder.Append((builder.Length == 0) ? string.Empty : "\r\n");
builder.Append(string.Format("[{0}]", ApplicationName));
}
if (!flag)
{
builder.Append(string.Format("\r\n{0}={1}", KeyName, Value));
}
StreamWriter writer = new StreamWriter(FileName, false, Encoding.Default);
writer.Write(builder);
writer.Close();
}
catch
{
}
}
#endregion
public string IniReadValue(string Section, string Key)
{
return this.GetPrivateProfileString(Section, Key, string.Empty, this.path);
}
public void IniWriteValue(string Section, string Key, string Value)
{
this.WritePrivateProfileString(Section, Key, Value, this.path);
}
}