删除一个ini文件's节及其数据VB.NET/C#

本文关键字:数据 VB NET 一个 文件 ini 删除 | 更新日期: 2023-09-27 18:04:58

如何在VB.NET中删除ini文件的节及其数据?实际上,我只能删除数据,而不能删除section的标题。

      rivate Declare Ansi Function WritePrivateProfileString _
      Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
      (ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, ByVal lpString As String, _
      ByVal lpFileName As String) As Integer

删除一个ini文件's节及其数据VB.NET/C#

使用WritePrivateProfileSection

为lpString参数传递NULL来清除整个section。

编辑:

测试代码:

Imports System.Runtime.InteropServices
Module Module1
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Private Function WritePrivateProfileSection(ByVal lpAppName As String, ByVal lpString As IntPtr, ByVal lpFileName As String) As Boolean
    End Function
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Private Function WritePrivateProfileString(ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Boolean
    End Function
    Sub Main()
        Dim res As Boolean
        res = WritePrivateProfileString("Test", "val1", "1", "D:'test.ini")
        res = WritePrivateProfileString("Test", "val2", "1", "D:'test.ini")
        res = WritePrivateProfileString("Test", "val3", "1", "D:'test.ini")
        res = WritePrivateProfileString("Test1", "val1", "1", "D:'test.ini")
        res = WritePrivateProfileString("Test1", "val2", "1", "D:'test.ini")
        res = WritePrivateProfileString("Test1", "val3", "1", "D:'test.ini")
        res = WritePrivateProfileSection("Test", IntPtr.Zero, "D:'test.ini")
    End Sub
End Module