0

I have the following ini configuration file:

[Section1]
key1 = value1
key2 = value2
key3 = value3
key4 = value4
[Section2]
key1 = value1
key2 = value2
key3 = value3
key4 = value4

The address for this document is (Application.StartupPath + "\demo.ini") Now I want to modify the Section name of "Section1"! Please forgive me for not finding a way to directly modify the Section name, I thought of an idea, although this idea seems troublesome, I just take out all the key-value pairs in Section1, and put them into a new Section name, such as Section9, and then I delete the entire Section1, so that I have achieved the purpose of modifying the Section name. I tried the following code, but I failed. It can only take out the first key-value pair in Section1, but the remaining key-value pairs cannot be taken out.

[Section1]
key1 = value1
key2 = value2
key3 = value3
key4 = value4
[Section2]
key1 = value1
key2 = value2
key3 = value3
key4 = value4
[Section9]
key1 = value1

How can i modify this code To achieve my purpose? Or is there any other better way to modify the Section name? I've been struggling all day over this,please help me,please

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Collections.Specialized.BitVector32;

namespace inipractise
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }



        private void Button1_Click(object sender, EventArgs e)
        {

            string keyValuePairs = IniFileHelper.GetSectionKeyValuePairs(Application.StartupPath + "\\demo.ini", "Section1");
            IniFileHelper.WriteSectionKeyValuePairs(Application.StartupPath + "\\demo.ini", "Section9", keyValuePairs);

        }

    }




    public static class IniFileHelper
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Ansi)]
        private static extern int GetPrivateProfileSection(string lpAppName, StringBuilder lpReturnedString, int nSize, string lpFileName);

        [DllImport("kernel32.dll", CharSet = CharSet.Ansi)]
        private static extern int WritePrivateProfileSection(string lpAppName, string lpString, string lpFileName);


        //GetSectionKeyValuePairs
        public static string GetSectionKeyValuePairs(string iniFilePath, string sectionName)
        {
            const int bufferSize = 32767;
            StringBuilder stringBuilder = new StringBuilder(bufferSize);
            GetPrivateProfileSection(sectionName, stringBuilder, bufferSize, iniFilePath);
            string keyValuePairs = stringBuilder.ToString().TrimEnd('\0'); 
            if (keyValuePairs.Length > 0)
            {
                keyValuePairs = keyValuePairs.Replace("\0", "="); 
                keyValuePairs = keyValuePairs.Replace("\0\n", "\r\n");
            }
            return keyValuePairs;
        }


        //WriteSectionKeyValuePairs
        public static void WriteSectionKeyValuePairs(string iniFilePath, string sectionName, string keyValuePairs)
        {
            const int bufferSize = 32767; 
            byte[] buffer = new byte[bufferSize];
            int length = Encoding.Default.GetBytes(keyValuePairs, 0, keyValuePairs.Length, buffer, 0);
            if (length >= bufferSize - 2) 
            {
                throw new ArgumentOutOfRangeException(nameof(keyValuePairs), "Key value pairs string is too long.");
            }
            buffer[length] = 0; 
            buffer[length + 1] = 0; 
            string keyValuePairsWithNullTerminator = Encoding.Default.GetString(buffer, 0, length + 2);
            WritePrivateProfileSection(sectionName, keyValuePairsWithNullTerminator, iniFilePath);
        }
    }

}
  • 1
    You can't use StringBuilder, it terminates the string conversion too soon. [Alternative](http://pinvoke.net/default.aspx/kernel32/GetPrivateProfileSection.html). Otherwise a good reason to stop using a feature that became obsolete and declared deprecated by Microsoft 30 years ago. – Hans Passant Apr 23 '23 at 09:18
  • You could write your own ini file handler (or maybe find a nuget for it). We've done that before, and since the file format is very simple, that's not really a big deal. – PMF Apr 23 '23 at 10:43
  • The following may be of interest: https://stackoverflow.com/questions/217902/reading-writing-an-ini-file – Tu deschizi eu inchid Apr 23 '23 at 16:21

0 Answers0