分享
 
 
 

.NET下INI配置文件操作类

王朝c#·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

using Microsoft.VisualBasic.CompilerServices;

using System;

using System.Collections;

using System.Runtime.InteropServices;

using System.Text;

namespace INIManage

{

/// <summary>

/// INIManage 的摘要说明

/// 在http://www.allapi.net/ 上发现了一个VB.NET的INI文件操作类,下载了看了看,顺手改成了C#版的

/// 你可以把它编译成dll在winform或webform中引用,也可以直接把代码拷到项目中使用

/// 我没有进行逐项测试,所以可能有不对的地方,请酌情修改

/// --------------丛兴滋(cncxz) 2005-08-23

/// </summary>

public class INIManage

{

#region" 引入相关dll "

[DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileIntA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]

private static extern int GetPrivateProfileInt(string lpApplicationName, string lpKeyName, int nDefault, string lpFileName);

[DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileSectionsNamesA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]

private static extern int GetPrivateProfileSectionsNames(byte[] lpszReturnBuffer, int nSize, string lpFileName);

[DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileStringA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]

private static extern int GetPrivateProfileString(string lpApplicationName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);

[DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileStructA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]

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);

[DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileStringA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]

private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);

[DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileStructA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]

private static extern int WritePrivateProfileStruct(string lpszSections, string lpszKey, byte[] lpStruct, int uSizeStruct, string szFile);

#endregion

private string _Filename; //INI文件名

private string _Sections; //INI文件中配置参数的组别片段

private const int MAX_ENTRY = 32768; //最大字符数

public INIManage(string strFile)

{

this.Filename = strFile;

}

#region" INI操作类的属性 "

public string Filename

{

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类型的配置参数。。。。依次类推

public bool ReadBoolean(string Key)

{

return this.ReadBoolean(this.Sections, Key);

}

public bool ReadBoolean(string Key, bool DefaultValue)

{

return this.ReadBoolean(this.Sections, Key, DefaultValue);

}

public bool ReadBoolean(string Sections, string Key)

{

return this.ReadBoolean(Sections, Key, false);

}

public bool ReadBoolean(string Sections, string Key, bool DefaultValue)

{

return bool.Parse(this.ReadString(Sections, Key, DefaultValue.ToString()));

}

public byte[] ReadByteArray(string Key, int Length)

{

return this.ReadByteArray(this.Sections, Key, Length);

}

public byte[] ReadByteArray(string Sections, string Key, int 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);

}

public int ReadInteger(string Key, int DefaultValue)

{

return this.ReadInteger(this.Sections, Key, DefaultValue);

}

public int ReadInteger(string Sections, string Key)

{

return this.ReadInteger(Sections, Key, 0);

}

public int ReadInteger(string Sections, string Key, int DefaultValue)

{

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;

}

public long ReadLong(string Key)

{

return this.ReadLong(Key, (long) 0);

}

public long ReadLong(string Key, long DefaultValue)

{

return this.ReadLong(this.Sections, Key, DefaultValue);

}

public long ReadLong(string Sections, string Key)

{

return this.ReadLong(Sections, Key, 0);

}

public long ReadLong(string Sections, string Key, long DefaultValue)

{

return long.Parse(this.ReadString(Sections, Key, DefaultValue.ToString()));

}

public string ReadString(string Key)

{

return this.ReadString(this.Sections, Key);

}

public string ReadString(string Sections, string Key)

{

return this.ReadString(Sections, Key, "");

}

public string ReadString(string Sections, string Key, string DefaultValue)

{

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;

}

#endregion

#region" INI操作类的Write相关方法 "

public bool Write(string Key, bool Value)

{

return this.Write(this.Sections, Key, Value);

}

public bool Write(string Key, byte[] Value)

{

return this.Write(this.Sections, Key, Value);

}

public bool Write(string Key, string Value)

{

return this.Write(this.Sections, Key, Value);

}

public bool Write(string Key, int Value)

{

return this.Write(this.Sections, Key, Value);

}

public bool Write(string Key, long Value)

{

return this.Write(this.Sections, Key, Value);

}

public bool Write(string Sections, string Key, byte[] 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;

}

public bool Write(string Sections, string Key, bool Value)

{

return this.Write(Sections, Key, Value.ToString());

}

public bool Write(string Sections, string Key, int Value)

{

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;

}

public bool Write(string Sections, string Key, long Value)

{

return this.Write(Sections, Key, Value.ToString());

}

public bool Write(string Sections, string Key, string Value)

{

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;

}

#endregion

#region" INI操作类的Delete相关方法 "

public bool DeleteKey(string Key)

{

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;

}

public bool DeleteKey(string Section, string Key)

{

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;

}

public bool DeleteSections(string Section)

{

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;

}

#endregion

public ArrayList GetSectionsNames()

{

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;

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有