[Personal use of. Net class library] (1) INI configuration file operation class,. net class library

Source: Internet
Author: User

[Personal use of. Net class library] (1) INI configuration file operation class,. net class library

When developing an interface program, the IP address and port configured for the interface program must be configurable, And the INI file read and write operations are implemented in the Windows Api native, therefore, you only need to call methods in the Win Api to operate the INI configuration file. The key code is how to call methods in the Win Api, as shown below:

# Region call the original method declaration of WinApi [DllImport ("kernel32")] private static extern long WritePrivateProfileString (string section, string key, string val, string filePath ); [DllImport ("kernel32")] private static extern int GetPrivateProfileString (string section, string key, string def, StringBuilder retVal, int size, string filePath ); [DllImport ("kernel32")] private static extern int GetPrivateProfileString (string section, string key, string defVal, Byte [] retVal, int size, string filePath); # endregion

The specific code is as follows (to delete the paragraph content, refer to the sufei Forum su Fei's great god ):

Using System; using System. text; using System. runtime. interopServices; namespace DotNetCommon. file {// <summary> /// class description: INI File read/write // The encoding person: Xiaojun // contact information: binghuojxj@qq.com /// </summary> public class IniFileHelper {// <summary> // INI file Path /// </summary> public string Path; /// <summary> /// block empty constructors /// </summary> public IniFileHelper () {throw new Exception ("Empty constructors are not allowed! ");} // <Summary> // constructor, the parameter is the INI file path // </summary> /// <param name = "path"> INI file path </param> public IniFileHelper (string path) {Path = path ;}# region call the original method declaration of WinApi [DllImport ("kernel32")] private static extern long WritePrivateProfileString (string section, string key, string val, string filePath ); [DllImport ("kernel32")] private static extern int GetPrivateProfileString (string section, string key, string def, StringBuilder retVal, int size, string filePath ); [DllImport ("kernel32")] private static extern int GetPrivateProfileString (string section, string key, string defVal, Byte [] retVal, int size, string filePath ); # endregion // <summary> // read the INI file // </summary> // <param name = "section"> section </param> // <param name = "key"> key </param> // <returns> </returns> public string IniReadValue (string section, string key) {var temp = new StringBuilder (255); var I = GetPrivateProfileString (section, key, "", temp, 255, Path); return temp. toString ();} /// <summary> /// write the INI file /// </summary> /// <param name = "section"> section </param> /// <param name = "key"> key </param> // <param name = "value"> value </param> public void IniWriteValue (string section, string key, string value) {WritePrivateProfileString (section, key, value, Path );} /// <summary> /// clear all paragraphs in the INI file // </summary> public void ClearAllSection () {IniWriteValue (null, null, null );} /// <summary> /// specify the section content in the INI file /// </summary> /// <param name = "section"> section </param> public void clearSection (string section) {IniWriteValue (section, null, null );}}}View Code
How to use C # To change the data value in the INI File

String path = Application. StartupPath + "\ config. ini ";
IniWriteValue (path, "config", "password", textBox1.Text. Trim ());

How does VB execute the code in the INI file? The most detailed comment

INI files never put code. Is the configuration file, there is no executable thing.
To execute the code of other files, you are writing a parser.


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.