Using Microsoft.VisualBasic.CompilerServices;
Using System;
Using System.Collections;
Using System.Runtime.InteropServices;
Using System.Text;
Namespace Inimanage
{
<summary>
Summary description of Inimanage
On the http://www.allapi.net/found a vb.net INI file Operation class, downloaded a look, conveniently changed to a C # version of
You can compile it into a DLL to reference in WinForm or WebForm, or you can copy the code directly to the project.
I have not carried out a test-by-item, so there may be some wrong place, please amend it as appropriate
--------------Congxing (CNCXZ) 2005-08-23
</summary>
public class Inimanage
{
#region "Introducing related DLLs"
[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 Lpfilenam e);
[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, S Tring 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 l Pfilename);
[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 file name
private string _sections; INI file group fragment for configuration parameters
Private Const int max_entry = 32768; Maximum number of characters
Public Inimanage (String strfile)
{
This. Filename = strfile;
}
#region the properties of the INI action class
public string Filename
{
Get
{
return this._filename;
}
Set
{
This._filename = value;
}
}
public string Sections
{
Get
{
return this._sections;
}
Set
{
This._sections = value;
}
}
#endregion
#region the Read correlation method of the INI operation class
The DefaultValue in the read correlation method is the return value when the associated configuration is not found in the INI file
Readboolean is reading a configuration parameter of type bool, Readbytearray is a configuration parameter reading a byte[] type
Readinteger is to read the configuration parameters of type int .... By analogy
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 operation class Write Related method"
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 the delete associated method for the INI action class
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;
&NBSP;&NBSP;&NBSP}
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;
&NBSP;&NBSP}
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 (' I ', (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;
}
}
}