Operate INI files in DOTNET

Source: Internet
Author: User
Tags dotnet

Using system;
Using system. IO;
Using system. collections;

Namespace sinoprise. Configuration
{
/// <Summary>
/// Read and set INIFILE
/// By Yin Shuguang
/// Www.sinoprise.com
/// </Summary>
Public class INIFILE
{

# Region external import function

// The write operation on the INI File
// [Dllimport ("Kernel32", charset = charset. Auto)]
// Protected static extern long writeprivateprofilestring (string section, string key, string Val, string filepath );

// The function used to read the INI File
// [Dllimport ("Kernel32", charset = charset. Auto)]
// Protected static extern int getprivateprofilestring (string section, string key, string def, stringbuilder retval, int size, string filepath );

# Endregion

Private string m_filename; // The path and name used to store the INI File
Private hashtable m_sections; // used to store the content of the entire INI File

Public String filename
{
Get
{
Return m_filename;
}
}

/// <Summary>
/// INIFILE Constructor
/// </Summary>
/// <Param name = "FILENAME"> path and name of the INI file </param>
Public INIFILE (string filename)
{
M_filename = filename;
M_sections = new hashtable ();
Loadvalues ();
}

~ INIFILE ()
{
M_sections = NULL;
}


/// <Summary>
/// Content of the INI File
/// </Summary>
Private void loadvalues ()
{
If (m_filename.trim ()! = "") & (File. exists (m_filename )))
{

Using (streamreader sr = new streamreader (m_filename ))
{
Setstrings (SR );
}

}
}


/// <Summary>
/// Add the section of an INI File
/// </Summary>
/// <Param name = "item"> section name </param>
/// <Returns> added section </returns>
Private hashtable addsection (string item)
{
Hashtable sectionsitem = new hashtable ();
M_sections.add (item, sectionsitem );
Return sectionsitem;
}

/// <Summary>
/// Read the INI file content to the memory
/// </Summary>
/// <Param name = "Sr"> Read the file stream </param>
Private void setstrings (streamreader SR)
{
String line;
Hashtable sectionskeys = NULL;
Int splitpos;
String keyname, keyValue;
While (line = Sr. Readline ())! = NULL)
{
Line = line. Trim ();

If (line = "") continue;

If (line. substring (0, 1 )! = ";")
{
If (line. substring (0, 1) = "[") & (line. substring (line. Length-1, 1) = "]")
{
Line = line. substring (1, line. Length-2 );
Sectionskeys = addsection (line. Trim ());
}

Else
{

Splitpos = line. indexof ('= ');
If (splitpos> 0) & (sectionskeys! = NULL ))
{
Keyname = line. substring (0, splitpos). Trim ();

If (keyname. Length = 0)
{
Throw new exception ("INIFILE syntax error! ");
}

If (keyname. Length <line. Length-1)
{
KeyValue = line. substring (splitpos plus 1, line. Length-2-splitPos );
}
Else
{
KeyValue = "";
}
Sectionskeys. Add (keyname, keyValue );
}

}
}

}
}

/// <Summary>
/// Write the content back to the INI File
/// </Summary>
Public void updatefile ()
{
Hashtable sectionsitem;

If (file. exists (m_filename ))
{
File. Delete (m_filename );
}

Using (streamwriter Sw = new streamwriter (m_filename ))
{

Foreach (dictionaryentry scnitem in m_sections)
{
Sw. writeline ("[" + (string) scnitem. Key + "]");

Sectionsitem = (hashtable) m_sections [(string) scnitem. Key];
Foreach (dictionaryentry keyitem in sectionsitem)
{
Sw. writeline (string) keyitem. Key + "=" + (string) keyitem. value );
}

Sw. writeline ("");
}

Sw. Close ();
}

}

/// <Summary>
/// Read all sections of the INI File
/// </Summary>
/// <Returns> return the section in string format in arraylist </returns>
Public arraylist readsections ()
{
Arraylist sectionlist = new arraylist ();

Foreach (dictionaryentry item in m_sections)
{
Sectionlist. Add (string) item. Key );
}
Return sectionlist;
}

/// <Summary>
/// Read all keys in a section of the INI File
/// </Summary>
/// <Param name = "sectionname"> section to be read </param>
/// <Returns> return the kry represented by a string in the arraylist form </returns>
Public arraylist readkeys (string sectionname)
{
Arraylist keylist = new arraylist ();

Hashtable sectionsitem = (hashtable) m_sections [sectionname. Trim ()];

Foreach (dictionaryentry item in sectionsitem)
{
Keylist. Add (string) item. Key );
}

Return keylist;
}

/// <Summary>
/// Determine whether a section exists
/// </Summary>
/// <Param name = "sectionname"> name of the section to be checked </param>
/// <Returns> If a section exists, true is returned; otherwise, false is returned. </returns>
Public bool sectionexists (string sectionname)
{
Return m_sections.containskey (sectionname. Trim ());
}

/// <Summary>
/// Determine whether a key exists in a section
/// </Summary>
/// <Param name = "sectionname"> specified section </param>
/// <Param name = "keyname"> name of the key to be checked </param>
/// <Returns> if this key exists in this section, true is returned; otherwise, false is returned. </returns>
Public bool keyexists (string sectionname, string keyname)
{
Sectionname = sectionname. Trim ();

If (! M_sections.containskey (sectionname ))
{
Return false;
}

Return (hashtable) m_sections [sectionname]). containskey (keyname. Trim ());
}

/// <Summary>
/// Delete a Section
/// </Summary>
/// <Param name = "sectionname"> name of the section to be deleted </param>
Public void erasesection (string sectionname)
{
M_sections.remove (sectionname );
}


/// <Summary>
/// Delete a key from a sectionk
/// </Summary>
/// <Param name = "sectionname"> specified section </param>
/// <Param name = "keyname"> name of the key to be deleted </param>
Public void deletekey (string sectionname, string keyname)
{
Sectionname = sectionname. Trim ();

If (! M_sections.containskey (sectionname ))
{
Return;
}

(Hashtable) m_sections [sectionname]). Remove (keyname. Trim ());
}

Public String readstring (string sectionname, string keyname, string defaultvalue)
{
Sectionname = sectionname. Trim ();
Keyname = keyname. Trim ();

If (! M_sections.containskey (sectionname ))
{
Return defaultvalue;
}

Hashtable sectionsitem = (hashtable) m_sections [sectionname];
If (! Sectionsitem. containskey (keyname ))
{
Return defaultvalue;
}

Return (string) sectionsitem [keyname];

}

Public void writestring (string sectionname, string keyname, string stringvalue)
{
Hashtable sectionsitem;

Sectionname = sectionname. Trim ();
Keyname = keyname. Trim ();
Stringvalue = stringvalue. Trim ();

If (! M_sections.containskey (sectionname ))
{
Sectionsitem = addsection (sectionname );
}
Else
{
Sectionsitem = (hashtable) m_sections [sectionname];
}


If (! Sectionsitem. containskey (keyname ))
{
Sectionsitem [keyname] = stringvalue;
}
Else
{
Sectionsitem. Add (keyname, stringvalue );
}
}


Public long readinteger (string sectionname, string keyname, long defaultvalue)
{
Return convert. toint64 (readstring (sectionname, keyname, convert. tostring (defaultvalue )));
}

Public void writeinteger (string sectionname, string keyname, long longvalue)
{
Writestring (sectionname, keyname, convert. tostring (longvalue ));
}



Public bool readbool (string sectionname, string keyname, bool defaultvalue)
{
Return convert. toboolean (readstring (sectionname, keyname, convert. tostring (defaultvalue )));
}

Public void writebool (string sectionname, string keyname, bool boolvalue)
{
Writestring (sectionname, keyname, convert. tostring (boolvalue ));
}


Public int readbinarystream (string sectionname, string keyname, stream defaultvalue)
{
Return 0;
}
Public void writebinarystream (string sectionname, string keyname, stream streamvalue)
{}


Public datetime readdate (string sectionname, string keyname, datetime defaultvalue)
{
Return convert. todatetime (readstring (sectionname, keyname, convert. tostring (defaultvalue). date;
}
Public void writedate (string sectionname, string keyname, datetime datevalue)
{
Writestring (sectionname, keyname, convert. tostring (datevalue. Date ));
}


Public datetime readdatetime (string sectionname, string keyname, datetime defaultvalue)
{
Return convert. todatetime (readstring (sectionname, keyname, convert. tostring (defaultvalue )));
}
Public void writedatetime (string sectionname, string keyname, datetime datetimevalue)
{
Writestring (sectionname, keyname, convert. tostring (datetimevalue ));
}


Public double readfloat (string sectionname, string keyname, double defaultvalue)
{
Return convert. todouble (readstring (sectionname, keyname, convert. tostring (defaultvalue )));
}
Public void writefloat (string sectionname, string keyname, double doublevalue)
{
Writestring (sectionname, keyname, convert. tostring (doublevalue ));
}

Public datetime readtime (string sectionname, string keyname, datetime defaultvalue)
{
Return convert. todatetime (readstring (sectionname, keyname, convert. tostring (defaultvalue )));
}
Public void writetime (string sectionname, string keyname, datetime timevalue)
{
Writestring (sectionname, keyname, convert. tostring (timevalue. timeofday ));
}


}
}

 

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.