SoFunction
Updated on 2025-03-07

C# sample code for reading and writing operations on INI files

1. Complete the reading, modification and writing operations of INI files by calling API functions

 string filePath;
        private void btnModify_Click(object sender, EventArgs e)
        {
            if ((filePath) || !(filePath))
            {
                using (SaveFileDialog sfd=new SaveFileDialog())
                {                    
                    = "Initialize the file(*.INI)|*.INI";
                    if (() == )
                    {
                        FileStream fs = ();
                        ();
                        filePath = ;
                    }
                }
            }
            (filePath, "ConnectString", "Data Source", );
            (filePath, "ConnectString", "DataBase", );
            (filePath, "ConnectString", "Uid", );
            (filePath, "ConnectString", "Pwd", );
            ("Save successfully!");
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
 
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            using(OpenFileDialog ofd=new OpenFileDialog())
            {
                 = "Initialize the file(*.INI)|*.INI";
                 = false;
                if (() == )
                {
                     filePath = ;
                    string source = (filePath, "ConnectString", "Data Source");
                    string dataName= (filePath, "ConnectString", "DataBase");
                    string uid= (filePath, "ConnectString", "Uid", "Not set");
                    string pwd= (filePath, "ConnectString", "Pwd","Not set");
                     = source;
                     = dataName;
                     = uid;
                     = pwd;
                }
            }
        }
    }
    /// <summary>
    /// Read and write INI files    /// </summary>
    class INIHelper
    {
        /// <summary>
        /// Read data from INI file        /// </summary>
        /// <param name="filePath">full path to INI file</param>        /// <param name="rootValue">Root node value, for example, the value of the root node [ConnectString] is: ConnectString</param>        /// <param name="key">key under the root node</param>        /// <param name="defValue">Default value when the tag value is not set or does not exist</param>        /// &lt;returns&gt;&lt;/returns&gt;
        public static string ReadFromINI(string filePath, string rootValue,string key,string defValue="")
        {
            StringBuilder sb = new StringBuilder(1024);
            GetPrivateProfileString(rootValue, key, defValue, sb, 1024, filePath);
            return ();
        }
        public static void WriteToINI(string filePath, string rootValue, string key, string newVal)
        {
           
            WritePrivateProfileString(rootValue, key, newVal, filePath);
           
        }
        
        /// &lt;summary&gt;
        /// Read the INI file        /// &lt;/summary&gt;
        /// <param name="IpAppName">represents the value of the root node inside the INI file</param>        /// <param name="IpKeyName">Denotes the value of the sub-marked root node</param>        /// <param name="IpDefault">Denotes the default value when the tag value is not set or does not exist</param>        /// <param name="IpReturnString">Return the value of the read node</param>        /// <param name="nSize">Maximum capacity of node content read</param>        /// <param name="IpFileName">Full path to file</param>        /// &lt;returns&gt;&lt;/returns&gt;
        [("kernel32")]
        static extern int GetPrivateProfileString(string IpAppName, string IpKeyName, string IpDefault, StringBuilder IpReturnString, int nSize, string IpFileName);
        /// &lt;summary&gt;
        /// Write to INI files        /// &lt;/summary&gt;
        /// <param name="mpAppName">value of the root node inside the INI file</param>        /// <param name="mpKeyName">The name of the tag to be modified</param>        /// <param name="mpDefault">What you want to modify</param>        /// <param name="mpFileName">full path to INI file</param>        /// &lt;returns&gt;&lt;/returns&gt;
        [("kernel32")]
        static extern long WritePrivateProfileString(string mpAppName, string mpKeyName, string mpDefault, string mpFileName);
    }

2. The extension of the INI file is .ini For example: .

3. The INI file format is as follows (open with Notepad):

[ConnectString]
Data Source=192.168.1.97
DataBase=master
Uid=sa
Pwd=1

annotation:

[ConnectString]: Root node. When reading and writing, you must first know the root node value (for example, [ConnectString] The root node value is: ConnectString)

DataBase: key, through which the corresponding value of the key can be obtained.

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