C# winform 如何使用[DllImport("kernel32.dll"]修改ini文件中的Section名称?

问题描述 投票:0回答:0

我有以下ini配置文件:

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

这个文件的地址是(Application.StartupPath + "\demo.ini") 现在我要修改“Section1”的Section名称! 请原谅我没有找到直接修改Section名称的方法, 我想到了一个想法,虽然这个想法看起来很麻烦,但我只是把Section1中的所有键值对都取出来,放到一个新的Section name中,比如Section9,然后我删除整个Section1,这样我就有了达到了修改Section名称的目的。我尝试了以下代码,但我失败了。只能取出Section1中的第一个键值对,其余的键值对不能取出

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

如何修改此代码以实现我的目的?或者有没有其他更好的方法来修改 Section 名称?我一整天都在为此苦苦挣扎,请帮助我

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);
        }
    }

}
c# api winforms dllimport ini
© www.soinside.com 2019 - 2024. All rights reserved.