VB怎么操作INI文件?写入+读取
答案:2 悬赏:50 手机版
解决时间 2021-03-03 17:22
- 提问者网友:遁入空寂
- 2021-03-02 21:27
RT 用API实现的
最佳答案
- 五星知识达人网友:罪歌
- 2021-03-02 22:56
Imports SystemImports System.Text
Imports System.Runtime.InteropServices
Namespace Lob_ini
Public Class cIni
Private ls_IniFilename As String
Private li_BufferLen As Integer = 256
'''
''' cINI Constructor
'''
Public Sub New(ByVal pIniFilename As String)
MyBase.New()
ls_IniFilename = pIniFilename
End Sub
'''
''' INI filename (If no path is specifyed the function will look with-in the windows directory for the file)
'''
Public Property IniFile() As String
Get
Return
End Get
Set(ByVal value As String)
ls_IniFilename = value
End Set
End Property
'''
''' Max return length when reading data (Max: 32767)
'''
Public Property BufferLen() As Integer
Get
Return li_BufferLen
End Get
Set(ByVal value As Integer)
If (value > 32767) Then
li_BufferLen = 32767
ElseIf (value < 1) Then
li_BufferLen = 1
Else
li_BufferLen = value
End If
End Set
End Property
Private Declare Function WritePrivateProfileStrin
g Lib "kernel32" (ByVal pSection As String, ByVal pKey As String, ByVal pValue As String, ByVal pFile As String) As Integer
Private Declare Function WritePrivateProfileStruc
t Lib "kernel32" (ByVal pSection As String, ByVal pKey As String, ByVal pValue As String, ByVal pValueLen As Integer, ByVal pFile As String) As Integer
Private Declare Function GetPrivateProfileString Lib "kernel32" (ByVal pSection As String, ByVal pKey As String, ByVal pDefault As String, ByVal prReturn() As Byte, ByVal pBufferLen As Integer, ByVal pFile As String) As Integer
Private Declare Function GetPrivateProfileStruct Lib "kernel32" (ByVal pSection As String, ByVal pKey As String, ByVal prReturn() As Byte, ByVal pBufferLen As Integer, ByVal pFile As String) As Integer
'''
''' Read value from INI File
'''
Public Overloads Function ReadValue(ByVal pSection As String, ByVal pKey As String, ByVal pDefault As String) As String
Return z_GetString(pSection, pKey, pDefault)
End Function
'''
''' Read value from INI File, default = ""
'''
Public Overloads Function ReadValue(ByVal pSection As String, ByVal pKey As String) As String
Return z_GetString(pSection, pKey, "")
End Function
'''
''' Write value to INI File
'''
Public Sub WriteValue(ByVal pSection As String, ByVal pKey As String, ByVal pValue As String)
WritePrivateProfileStrin
g(pSection, pKey, pValue, Me.ls_IniFilename)
End Sub
'''
''' Remove value from INI File
'''
Public Sub RemoveValue(ByVal pSection As String, ByVal pKey As String)
WritePrivateProfileStrin
g(pSection, pKey, Nothing, Me.ls_IniFilename)
End Sub
'''
''' Read values in a section from INI File
'''
Public Sub ReadValues(ByVal pSection As String, ByRef pValues As Array)
pValues = z_GetString(pSection, Nothing, Nothing).Split(CType(ChrW(0), Char))
End Sub
'''
''' Read sections from INI File
'''
Public Sub ReadSections(ByRef pSections As Array)
pSections = z_GetString(Nothing, Nothing, Nothing).Split(CType(ChrW(0), Char))
End Sub
'''
''' Remove section from INI File
'''
Public Sub RemoveSection(ByVal pSection As String)
WritePrivateProfileStrin
g(pSection, Nothing, Nothing, Me.ls_IniFilename)
End Sub
'''
''' Call GetPrivateProfileString / GetPrivateProfileStruct API
'''
Private Function z_GetString(ByVal pSection As String, ByVal pKey As String, ByVal pDefault As String) As String
Dim sRet As String = pDefault
Dim bRet() As Byte = New Byte((li_BufferLen) - 1) {}
Dim i As Integer = GetPrivateProfileString(pSection, pKey, pDefault, bRet, li_BufferLen, ls_IniFilename)
sRet = System.Text.Encoding.GetEncoding(1252).GetString(bRet, 0, i).TrimEnd(CType(ChrW(0), Char))
Return sRet
End Function
End Class
End Namespace
Imports System.Runtime.InteropServices
Namespace Lob_ini
Public Class cIni
Private ls_IniFilename As String
Private li_BufferLen As Integer = 256
'''
''' cINI Constructor
'''
Public Sub New(ByVal pIniFilename As String)
MyBase.New()
ls_IniFilename = pIniFilename
End Sub
'''
''' INI filename (If no path is specifyed the function will look with-in the windows directory for the file)
'''
Public Property IniFile() As String
Get
Return
End Get
Set(ByVal value As String)
ls_IniFilename = value
End Set
End Property
'''
''' Max return length when reading data (Max: 32767)
'''
Public Property BufferLen() As Integer
Get
Return li_BufferLen
End Get
Set(ByVal value As Integer)
If (value > 32767) Then
li_BufferLen = 32767
ElseIf (value < 1) Then
li_BufferLen = 1
Else
li_BufferLen = value
End If
End Set
End Property
Private Declare Function WritePrivateProfileStrin
g Lib "kernel32" (ByVal pSection As String, ByVal pKey As String, ByVal pValue As String, ByVal pFile As String) As Integer
Private Declare Function WritePrivateProfileStruc
t Lib "kernel32" (ByVal pSection As String, ByVal pKey As String, ByVal pValue As String, ByVal pValueLen As Integer, ByVal pFile As String) As Integer
Private Declare Function GetPrivateProfileString Lib "kernel32" (ByVal pSection As String, ByVal pKey As String, ByVal pDefault As String, ByVal prReturn() As Byte, ByVal pBufferLen As Integer, ByVal pFile As String) As Integer
Private Declare Function GetPrivateProfileStruct Lib "kernel32" (ByVal pSection As String, ByVal pKey As String, ByVal prReturn() As Byte, ByVal pBufferLen As Integer, ByVal pFile As String) As Integer
'''
''' Read value from INI File
'''
Public Overloads Function ReadValue(ByVal pSection As String, ByVal pKey As String, ByVal pDefault As String) As String
Return z_GetString(pSection, pKey, pDefault)
End Function
'''
''' Read value from INI File, default = ""
'''
Public Overloads Function ReadValue(ByVal pSection As String, ByVal pKey As String) As String
Return z_GetString(pSection, pKey, "")
End Function
'''
''' Write value to INI File
'''
Public Sub WriteValue(ByVal pSection As String, ByVal pKey As String, ByVal pValue As String)
WritePrivateProfileStrin
g(pSection, pKey, pValue, Me.ls_IniFilename)
End Sub
'''
''' Remove value from INI File
'''
Public Sub RemoveValue(ByVal pSection As String, ByVal pKey As String)
WritePrivateProfileStrin
g(pSection, pKey, Nothing, Me.ls_IniFilename)
End Sub
'''
''' Read values in a section from INI File
'''
Public Sub ReadValues(ByVal pSection As String, ByRef pValues As Array)
pValues = z_GetString(pSection, Nothing, Nothing).Split(CType(ChrW(0), Char))
End Sub
'''
''' Read sections from INI File
'''
Public Sub ReadSections(ByRef pSections As Array)
pSections = z_GetString(Nothing, Nothing, Nothing).Split(CType(ChrW(0), Char))
End Sub
'''
''' Remove section from INI File
'''
Public Sub RemoveSection(ByVal pSection As String)
WritePrivateProfileStrin
g(pSection, Nothing, Nothing, Me.ls_IniFilename)
End Sub
'''
''' Call GetPrivateProfileString / GetPrivateProfileStruct API
'''
Private Function z_GetString(ByVal pSection As String, ByVal pKey As String, ByVal pDefault As String) As String
Dim sRet As String = pDefault
Dim bRet() As Byte = New Byte((li_BufferLen) - 1) {}
Dim i As Integer = GetPrivateProfileString(pSection, pKey, pDefault, bRet, li_BufferLen, ls_IniFilename)
sRet = System.Text.Encoding.GetEncoding(1252).GetString(bRet, 0, i).TrimEnd(CType(ChrW(0), Char))
Return sRet
End Function
End Class
End Namespace
全部回答
- 1楼网友:怙棘
- 2021-03-03 00:28
读ini文件的函数
public function getinifile(byval appname as string, byval keyname as string) as string
on error goto getinifileerr
dim retstr as string
retstr = string(255, chr(0))
getinifile = trim((left(retstr, getprivateprofilestring(appname, keyname, "", retstr, len(retstr), filename))))
exit function
getinifileerr:
msgbox "读文件时发生错误!" & chr(13) & "错误代码:" & err.number & chr(13) & "错误描述:" & err.description, vbexclamation + vbokonly, "读文件错误"
end function
'写ini文件的函数
public function writeinifile(byval appname as string, byval keyname as string, byval key as string) as boolean
dim longwrite as long
on error goto writeinifileerr
longwrite = writeprivateprofilestring(appname, keyname, key, filename)
exit function
writeinifileerr:
msgbox "写文件时发生错误!" & chr(13) & "错误代码:" & err.number & chr(13) & "错误描述:" & err.description, vbexclamation + vbokonly, "写文件错误"
end function
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯