c#INI file

INI file

The .ini file is the abbreviation of Initialization File, which is the storage format used by Windows system configuration files. It manages various configurations of Windows. Generally, users can use the various graphical management interfaces provided by Windows to achieve the same Configured. But in some cases, it is more convenient to edit the ini directly. Generally, only those who are familiar with windows can edit directly.

INI file format

INI files consist of sections, keys, and values.

Festival

[section]

parameter

(key=value)

name=value

annotation

Annotations are represented by semicolons (;). The text after the semicolon until the end of the line is a comment.

example:

[Student]
Name=张三
City=郑州
Age=22

 Write to ini file

If it exists, modify it

 /*
        引用c/c++语言的dll时,方法名不是随便命名的,要跟c/c++ 的dll里面的方法名一样
        */
        /// <summary>
        /// ini配置文件写入配置
        /// 如果存在就修改,如果不存在就新增
        /// </summary>
        /// <param name="sectionName">段落名</param>
        /// <param name="keyName">键</param>
        /// <param name="value">值</param>
        /// <param name="fileName">ini文件路径</param>
        [DllImport("Kernel32.dll")]
        public static extern Int32 WritePrivateProfileString(
    string sectionName,
    string keyName,
    string value,
    string fileName);

Read ini setting file

  /// <summary>
        /// ini配置文件读取配置
        /// </summary>
        /// <returns></returns>
        /// <param name="sectionName">段落名</param>
        /// <param name="keyName">键</param>
        /// <param name="defaultValue">当键不存在时返回的默认值</param>
        /// <param name="returnValue">当前读取到的数据</param>
        /// <param name="seze">需要读取数据的大小</param>
        /// <param name="fileName">ini文件的路径</param>
        [DllImport("kernel32.dll")]
        public static extern Int32 GetPrivateProfileString(
        string sectionName,
        string keyName,
        string defaultValue,
        StringBuilder returnValue,
        UInt32 seze,
        string fileName
        );

Guess you like

Origin blog.csdn.net/qq_57212959/article/details/131557572