SoFunction
Updated on 2025-03-08

Sample code for C# operating INI files

using System;
using ;
using ;
using ;
using ;
using ;

namespace xxxxx
{
    class IniFileOp
    {
        //#region Declares API functions for reading and writing INI files        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
        //Parameter description: section: paragraphs in INI file; key: keywords in INI file; val: value of keywords in INI file; filePath: complete path and name of INI file.
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        //Parameter description: section: paragraph name in INI file; key: keyword in INI file; def: default value when it cannot be read; retVal: read value; size: size of value; filePath: full path and name of INI file        //#endregion

        public string Path;

        public IniFileOp(string path)
        {
             = path;
        }


        /// <summary>
        /// Write INI files        /// </summary>
        /// <param name="section">paragraph</param>        /// <param name="key">key</param>        /// <param name="iValue">value</param>        public void IniWriteStr(string section, string key, string iValue)
        {
            WritePrivateProfileString(section, key, iValue, );
        }

        /// &lt;summary&gt;
        /// Read the INI file        /// &lt;/summary&gt;
        /// <param name="section">paragraph</param>        /// <param name="key">key</param>        /// <returns>Return key value</returns>        public string IniReadStr(string section, string key)
        {
            StringBuilder sb = new StringBuilder(256);

            int i = GetPrivateProfileString(section, key, "", sb, 256, );
            return ();
        }

        /// &lt;summary&gt;
        /// Read the INI file        /// &lt;/summary&gt;
        /// <param name="Section">Segment, format[]</param>        /// <param name="Key">key</param>        /// <returns>Returns section group or key-value group of type byte</returns>        public byte[] IniReadBytes(string section, string key)
        {
            StringBuilder sb = new StringBuilder(256);

            int i = GetPrivateProfileString(section, key, "", sb, 256, );
            string s = ();
            byte[] byteArray = (s);
            return byteArray;
        }
    }
}

[Param]
PauseTime=0

use

 int pauseTime = 0;

 string path1 = ;
 path1 += "";
 IniFileOp inifile = new IniFileOp(path1);

 string str1 = ("Param", "PauseTime");

 if (str1 != "")
 {
     pauseTime = (str1);
 }

This is the article about the sample code of C# operation INI file. For more related C# operation INI content, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!