INI file reads under the. NET Compact Framework set (C #)

Source: Internet
Author: User
Tags compact ini split trim

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
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.