using Microsoft.VisualBasic.CompilerServices; namespace INIManage #region" 引入相关dll " [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileIntA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)] [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileSectionsNamesA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)] [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileStringA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)] [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileStructA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)] [DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileStringA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)] [DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileStructA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)] #endregion #region" INI操作类的属性 " public string Filename public bool ReadBoolean(string Key) public bool ReadBoolean(string Key, bool DefaultValue) public bool ReadBoolean(string Sections, string Key) public bool ReadBoolean(string Sections, string Key, bool DefaultValue) public byte[] ReadByteArray(string Key, int Length) public byte[] ReadByteArray(string Sections, string Key, int Length) public int ReadInteger(string Key, int DefaultValue) public int ReadInteger(string Sections, string Key) public int ReadInteger(string Sections, string Key, int DefaultValue) public long ReadLong(string Key) public long ReadLong(string Key, long DefaultValue) public long ReadLong(string Sections, string Key) public long ReadLong(string Sections, string Key, long DefaultValue) public string ReadString(string Key) public string ReadString(string Sections, string Key) public string ReadString(string Sections, string Key, string DefaultValue) #endregion public bool Write(string Key, byte[] Value) public bool Write(string Key, string Value) public bool Write(string Key, int Value) public bool Write(string Key, long Value) public bool Write(string Sections, string Key, byte[] Value) public bool Write(string Sections, string Key, bool Value) public bool Write(string Sections, string Key, int Value) public bool Write(string Sections, string Key, long Value) public bool Write(string Sections, string Key, string Value) #endregion public bool DeleteKey(string Key) public bool DeleteKey(string Section, string Key) public bool DeleteSections(string Section) #endregion public ArrayList GetSectionsNames() }
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;
{
/// <summary>
/// INIManage 的摘要说明
/// 在http://www.allapi.net/ 上发现了一个VB.NET的INI文件操作类,下载了看了看,顺手改成了C#版的
/// 你可以把它编译成dll在winform或webform中引用,也可以直接把代码拷到项目中使用
/// 我没有进行逐项测试,所以可能有不对的地方,请酌情修改
/// --------------丛兴滋(cncxz) 2005-08-23
/// </summary>
public class INIManage
{
private static extern int GetPrivateProfileInt(string lpApplicationName, string lpKeyName, int nDefault, string lpFileName);
private static extern int GetPrivateProfileSectionsNames(byte[] lpszReturnBuffer, int nSize, string lpFileName);
private static extern int GetPrivateProfileString(string lpApplicationName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
private static extern int GetPrivateProfileStruct(string lpszSections, string lpszKey, byte[] lpStruct, int uSizeStruct, string szFile);
[DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileSectionsA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
private static extern int WritePrivateProfileSections(string lpAppName, string lpString, string lpFileName);
private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);
private static extern int WritePrivateProfileStruct(string lpszSections, string lpszKey, byte[] lpStruct, int uSizeStruct, string szFile);
private string _Filename; //INI文件名
private string _Sections; //INI文件中配置参数的组别片段
private const int MAX_ENTRY = 32768; //最大字符数
public INIManage(string strFile)
{
this.Filename = strFile;
}
{
get
{
return this._Filename;
}
set
{
this._Filename = value;
}
}
public string Sections
{
get
{
return this._Sections;
}
set
{
this._Sections = value;
}
}
#endregion
#region" INI操作类的Read相关方法 "
// Read相关方法中的 DefaultValue是 在INI文件中找不到相关配置 时的返回值
//ReadBoolean是读取bool类型的配置参数,ReadByteArray是读取 byte[]类型的配置参数
//ReadInteger是读取int类型的配置参数。。。。依次类推
{
return this.ReadBoolean(this.Sections, Key);
}
{
return this.ReadBoolean(this.Sections, Key, DefaultValue);
}
{
return this.ReadBoolean(Sections, Key, false);
}
{
return bool.Parse(this.ReadString(Sections, Key, DefaultValue.ToString()));
}
{
return this.ReadByteArray(this.Sections, Key, Length);
}
{
byte[] buffer1;
if (Length > 0)
{
try
{
byte[] buffer2 = new byte[(Length - 1) + 1];
if (INIManage.GetPrivateProfileStruct(Sections, Key, buffer2, buffer2.Length, this.Filename) == 0)
{
return null;
}
return buffer2;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
buffer1 = null;
ProjectData.ClearProjectError();
return buffer1;
}
}
else
{
return null;
}
}
public int ReadInteger(string Key)
{
return this.ReadInteger(Key, 0);
}
{
return this.ReadInteger(this.Sections, Key, DefaultValue);
}
{
return this.ReadInteger(Sections, Key, 0);
}
{
int num1;
try
{
num1 = INIManage.GetPrivateProfileInt(Sections, Key, DefaultValue, this.Filename);
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
num1 = DefaultValue;
ProjectData.ClearProjectError();
return num1;
}
return num1;
}
{
return this.ReadLong(Key, (long) 0);
}
{
return this.ReadLong(this.Sections, Key, DefaultValue);
}
{
return this.ReadLong(Sections, Key, 0);
}
{
return long.Parse(this.ReadString(Sections, Key, DefaultValue.ToString()));
}
{
return this.ReadString(this.Sections, Key);
}
{
return this.ReadString(Sections, Key, "");
}
{
string text1;
try
{
StringBuilder builder1 = new StringBuilder(MAX_ENTRY);
int num1 = INIManage.GetPrivateProfileString(Sections, Key, DefaultValue, builder1, MAX_ENTRY, this.Filename);
text1 = builder1.ToString();
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
text1 = DefaultValue;
ProjectData.ClearProjectError();
return text1;
}
return text1;
}
#region" INI操作类的Write相关方法 "
public bool Write(string Key, bool Value)
{
return this.Write(this.Sections, Key, Value);
}
{
return this.Write(this.Sections, Key, Value);
}
{
return this.Write(this.Sections, Key, Value);
}
{
return this.Write(this.Sections, Key, Value);
}
{
return this.Write(this.Sections, Key, Value);
}
{
bool flag1;
try
{
flag1 = INIManage.WritePrivateProfileStruct(Sections, Key, Value, Value.Length, this.Filename) != 0;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
flag1 = false;
ProjectData.ClearProjectError();
return flag1;
}
return flag1;
}
{
return this.Write(Sections, Key, Value.ToString());
}
{
bool flag1;
try
{
flag1 = INIManage.WritePrivateProfileString(Sections, Key, Value.ToString(), this.Filename) != 0;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
flag1 = false;
ProjectData.ClearProjectError();
return flag1;
}
return flag1;
}
{
return this.Write(Sections, Key, Value.ToString());
}
{
bool flag1;
try
{
flag1 = INIManage.WritePrivateProfileString(Sections, Key, Value, this.Filename) != 0;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
flag1 = false;
ProjectData.ClearProjectError();
return flag1;
}
return flag1;
}
#region" INI操作类的Delete相关方法 "
{
bool flag1;
try
{
flag1 = INIManage.WritePrivateProfileString(this.Sections, Key, null, this.Filename) != 0;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
flag1 = false;
ProjectData.ClearProjectError();
return flag1;
}
return flag1;
}
{
bool flag1;
try
{
flag1 = INIManage.WritePrivateProfileString(Sections, Key, null, this.Filename) != 0;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
flag1 = false;
ProjectData.ClearProjectError();
return flag1;
}
return flag1;
}
{
bool flag1;
try
{
flag1 = INIManage.WritePrivateProfileSections(Sections, null, this.Filename) != 0;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
flag1 = false;
ProjectData.ClearProjectError();
return flag1;
}
return flag1;
}
{
int num1;
ArrayList list1 = new ArrayList();
byte[] buffer1 = new byte[MAX_ENTRY];
int num2 = 0;
try
{
num1 = INIManage.GetPrivateProfileSectionsNames(buffer1, MAX_ENTRY, this.Filename);
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
ProjectData.ClearProjectError();
return list1;
}
ASCIIEncoding encoding1 = new ASCIIEncoding();
if (num1 > 0)
{
string text1 = encoding1.GetString(buffer1);
num1 = 0;
num2 = -1;
while (true)
{
num1 = text1.IndexOf('\0', (int) (num2 + 1));
if (((num1 - num2) == 1) || (num1 == -1))
{
return list1;
}
try
{
list1.Add(text1.Substring(num2 + 1, num1 - num2));
}
catch (Exception exception2)
{
ProjectData.SetProjectError(exception2);
ProjectData.ClearProjectError();
}
num2 = num1;
}
}
return list1;
}
}
