[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.