Class IniFile
#Region "声明读写INI文件的API函数"
'读文件
''' <summary>
''' 读INI文件
''' </summary>
''' <param name="lpApplicationName">欲查找的条目的小节名称</param>
''' <param name="lpKeyName">关键字</param>
''' <param name="lpDefault">指定的条目没有找到时的返回值</param>
''' <param name="lpReturnedString">字符串缓冲区大小不能小于nSize</param>
''' <param name="nSize">指定装载到lpReturnedString缓冲区的最大字符数量</param>
''' <param name="lpFileName">文件路径</param>
''' <returns>返回lpReturnedString中的字符数量</returns>
''' <remarks></remarks>
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As System.Text.StringBuilder, ByVal nSize As Int32, ByVal lpFileName As String) As Long
'写文件
''' <summary>
''' 写INI文件
''' </summary>
''' <param name="lpApplicationName">欲写入的条目的小节名称</param>
''' <param name="lpKeyName">关键字</param>
''' <param name="lpString">欲写入的字符串</param>
''' <param name="lpFileName">文件路径</param>
''' <returns>返回0表示失败,非0表示成功</returns>
''' <remarks></remarks>
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
'Public Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
#End Region
''' <summary>
''' 读INI
''' </summary>
''' <param name="ApplicationName">INI中的节点</param>
''' <param name="KeyName">节点中的键</param>
''' <param name="DefaultValue">找不到时返回的值</param>
''' <param name="FilePath">INI文件路径,一定要写完整</param>
''' <returns>读到的值</returns>
''' <remarks></remarks>
Public Function ReadIniFile(ByVal ApplicationName As String, ByVal KeyName As String, ByVal DefaultValue As String, ByVal FilePath As String) As String
If File.Exists(FilePath) Then
Dim resultstring As New System.Text.StringBuilder(1024)
GetPrivateProfileString(ApplicationName, KeyName, DefaultValue, resultstring, 1024, FilePath)
Return resultstring.ToString
Else
Return Nothing
End If
End Function
''' <summary>
''' 写INI
''' </summary>
''' <param name="ApplicationName">INI中的节点</param>
''' <param name="KeyName">节点的键</param>
''' <param name="KeyValue">要写的键值</param>
''' <param name="FilePath">INI文件路径,一定要写完整</param>
''' <returns></returns>
''' <remarks>可以给已经存在的键赋值,也可以同时创建键</remarks>
Public Function WriteFile(ByVal ApplicationName As String, ByVal KeyName As String, ByVal KeyValue As String, ByVal FilePath As String) As Boolean
If File.Exists(FilePath) Then
Dim i As Long = WritePrivateProfileString(ApplicationName, KeyName, KeyValue, FilePath)
If i = 0 Then
Return False
Else
Return True
End If
Else
Return False
End If
End Function
''' <summary>
''' 删除指定的键
''' </summary>
''' <param name="ApplicationName">键所在的节点</param>
''' <param name="KeyName">键名</param>
''' <param name="FilePath">INI文件路径</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function DelKey(ByVal ApplicationName As String, ByVal KeyName As String, ByVal FilePath As String) As Boolean
If File.Exists(FilePath) Then
Dim i As Long = WritePrivateProfileString(ApplicationName, KeyName, Nothing, FilePath)
If i = 0 Then
Return False
Else
Return True
End If
Else
Return False
End If
End Function
''' <summary>
''' 删除节点
''' </summary>
''' <param name="ApplicationName">节点名</param>
''' <param name="FilePath">INI文件路径</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function DelApplication(ByVal ApplicationName As String, ByVal FilePath As String) As Boolean
If File.Exists(FilePath) Then
Dim i As Long = WritePrivateProfileString(ApplicationName, Nothing, Nothing, FilePath)
If i = 0 Then
Return False
Else
Return True
End If
Else
Return False
End If
End Function
End Class