INI file read (C #)

Source: Internet
Author: User
Tags ini split trim
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; The key match is not found, the default value is returned
}
}
return Default; Return default values
}


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.