删除ini文件的部分及其数据VB.NET/C#

时间:2015-07-28 08:21:10

标签: c# vb.net ini

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

      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

1 个答案:

答案 0 :(得分:3)

使用WritePrivateProfileSection

要清除整个部分,请为lpString参数传递NULL。

编辑:

经过测试的代码:

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