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;
}
}
}
新闻热点
疑难解答
图片精选