C # using INI files under WinCE (bug correction Version)

Source: Internet
Author: User
Tags inif

I have been searching for N for a long time on the Internet, but I cannot find a suitable one. I spent a few hours writing it today and I will use it later. After wince debugging, ensure normal operation. To facilitate other users, we will announce the following:

/// Start the entire unit

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Using system. collections;

Namespace sjanalyzer // name of my space. You can change it.
{
Class ceinifiles
{
String inifilename;
Char [] trimchar = {'', '\ t '};

Public ceinifiles (string inif)
{

Inifilename = inif;
}

Public String [] getsects ()
{
String [] sects = NULL;

If (file. exists (inifilename ))
{
String STR;
Arraylist ls = new arraylist ();
Textreader TR = file. opentext (inifilename );
While (STR = tr. Readline ())! = NULL)
{
STR = Str. Trim ();
If (Str. startswith ("[") & (Str. endswith ("]")
Ls. Add (STR );
}
Tr. Close ();

If (LS. Count> 0)
{
Sects = new string [ls. Count];
For (INT I = 0; I <ls. Count; I ++)
{
Sects [I] = ls [I]. tostring ();
}
}
}

Return sects;
}

Public int writestring (string sect, string keystr, string valuestr)
{
Arraylist ls = new arraylist ();
Bool sectok = false;
Bool setok = false;

If (file. exists (inifilename ))
{
Int pos1;
String substr;
String STR;
Textreader TR = file. opentext (inifilename );
While (STR = tr. Readline ())! = NULL)
{
Ls. Add (STR );
}
Tr. Close ();

// Start searching for keywords. If the keyword cannot be found, insert it in the last line of the Section, and then save the INI file as a whole.
For (INT I = 0; I <ls. Count; I ++)
{
STR = ls [I]. tostring ();

If (Str. startswith ("[") & sectok) // first judge whether it is in the next segment. If it is the last segment, it may never happen.
{
Setok = true; // if it is not found in this section and is about to enter the next section, add it directly at the end of this section.
Ls. insert (I, keystr. Trim () + "=" + valuestr );
Break; // if it reaches the next segment, exit directly.
}
If (sectok)
{
Pos1 = Str. indexof ("= ");
If (pos1> 1)
{
Substr = Str. substring (0, pos1 );
Substr. Trim (trimchar );
// If the key is found in this section, modify it directly.
If (substr. Equals (keystr, stringcomparison. ordinalignorecase) & sectok) // it is in this segment, and the front segment of keystr can also match.
{
Setok = true;
Ls [I] = keystr. Trim () + "=" + valuestr;
Break;
}
}
}

If (Str. startswith ("[" + sect + "]") // you can determine whether the specified segment is in the required segment.
Sectok = true;
}

If (setok = false)
{
Setok = true;
If (! Sectok) // if no segment is found, you need to add another segment.
{
Ls. Add ("[" + sect + "]");
}
Ls. Add (keystr. Trim () + "=" + valuestr );
}
} // If the file does not exist, you need to create a file.
Else
{
Ls. Clear ();
Ls. Add ("# File Creation:" + datetime. Now. tostring () + "##");
Ls. Add ("[" + sect + "]");
Ls. Add (keystr. Trim () + "=" + valuestr );
}

If (file. exists (inifilename) // Delete the source file.
{
File. Delete (inifilename );
}

Textwriter Tw = file. createtext (inifilename );

// String [] strlist = new string [ls. Count];

For (INT I = 0; I <ls. Count; I ++)
{
// Strlist [I] = ls [I]. tostring ();
Tw. writeline (LS [I]. tostring ());
}
Tw. Flush ();
Tw. Close ();

// File. writealllines (inifilename, strlist );
Return 0;
}

Public String readstring (string sect, string keystr, string defaultstr)
{
String retstr = defaultstr;
If (file. exists (inifilename ))
{
Bool sectok = false;
Int pos1;
String substr;
String STR;
Arraylist ls = new arraylist ();
Textreader TR = file. opentext (inifilename );
While (STR = tr. Readline ())! = NULL)
{
STR = Str. Trim ();
If (Str. startswith ("[") & sectok) // first judge whether it is in the next section.
{
Break; // if it reaches the next segment, exit directly.
}
If (sectok)
{
Pos1 = Str. indexof ("= ");
If (pos1> 1)
{
Substr = Str. substring (0, pos1 );
Substr. Trim (trimchar );

If (substr. Equals (keystr, stringcomparison. ordinalignorecase) // it is in this segment and can be matched in the previous segment of keystr.
{
Retstr = Str. substring (pos1 + 1). Trim (trimchar );
Break;
}
}
}

If (Str. startswith ("[" + sect + "]") // you can determine whether the specified segment is in the required segment.
Sectok = true;

}
Tr. Close ();
}

Return retstr;
}

// Read integer
Public int readinteger (string section, string Ident, int default)
{
String intstr = readstring (section, ident, convert. tostring (default ));
Try
{
Return convert. toint32 (intstr );
}
Catch
{
Return default;
}
}

// Write an integer
Public void writeinteger (string section, string Ident, int value)
{
Writestring (section, ident, value. tostring ());
}

// Read Boolean
Public bool readbool (string section, string Ident, bool default)
{
Try
{
Return convert. toboolean (readstring (section, ident, convert. tostring (default )));
}
Catch
{
Return default;
}
}

// Write bool
Public void writebool (string section, string Ident, bool value)
{
Writestring (section, ident, convert. tostring (value ));
}

//////////////////////////////////////// /////////////////////////////////
// Special example of using this INI file (for your own use)
Public String getparam (string keystr, string default)
{
String STR;
STR = This. readstring ("Params", keystr ,"??? ");
If (STR = "??? ")
{
This. writestring ("Params", keystr, default );
STR = default;
}
Return STR;
}

Public void updateparam (string keystr, string valuestr)
{
This. writestring ("Params", keystr, valuestr );
}
}
}

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.