Both the. NET Framework and the. NET Compact Framework set are perfect for supporting XML files, and Microsoft strongly recommends replacing the INI file with an XML file, but most of the projects in the industrial area are based on the INI file configuration system information.
The former tunnel management system is the use of INI file configuration information, in order to achieve compatibility, but also in the lower computer with INI file configuration information. Since the WinCE platform does not provide the INI file read API function GetPrivateProfileString, so need to write, a period of time ago to provide a EVC version of the INI file read, and later on the da66x wince platform all using C # programming, So it is not possible to develop a C # version of the INI read function.
This is my original project code based on excerpts of an INI read function.
Read the INI file;
private String GetPrivateProfileString (string applicationname, String keyname,string Default, String FileName)
{
string[] Iniitems = new String[0];
String Inilines;
String Iniline;
int I, J;
Try
{
Read the INI file;
System.IO.StreamReader iniFile = new System.IO.StreamReader (FileName, System.Text.Encoding.Default);
Inilines = Inifile.readtoend ();
Inifile.close ();
}
Catch
{
return Default;
}
Split by carriage return to get each row
Iniitems = Inilines.split (");
Traverse each row
for (i = 0; i < iniitems.getlength (0); i++)
{
Matching value found
if (iniitems[i). Trim (). ToUpper () = = ' [' + Applicationname.trim (). ToUpper () + '] ')
{
Start the search from the next line
for (j = i + 1; j < iniitems.getlength (0); j + +)
{
Iniline = Iniitems[j]. Trim ();
if (Iniline.length > 0)
{
If another segment is found, the paragraph is over and the default value is returned
if (iniline[0] = = ' [' && iniline[iniline.length-1] = = '] return Default;
}
Remove all spaces
Iniline = Iniitems[j]. TrimStart (). Replace ("", "");
if (iniline.substring (0, Math.min (keyname.length + 1, iniline.length)). ToUpper () = = Keyname.toupper () + "=")
{
If a key match is found
Return INIITEMS[J]. Substring (Iniitems[j]. IndexOf (' = ') + 1);
}
}
Return default;//did not find a key match, the default value is returned
}
}
Return default;//returns the default value
}