永发信息网

VB怎么操作INI文件?写入+读取

答案:2  悬赏:50  手机版
解决时间 2021-03-03 17:22
RT 用API实现的
最佳答案
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
全部回答
读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
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
25岁用什么护肤品好 水 乳之后的步骤是什么
淄博市淄川区音乐家协会怎么去啊,有知道地址
大清宝钞模板值多少钱
康佳彩电图纸大全那个网站可以找到?
家里一开空调就断电(所有电器都没电),总闸
将具有1对相对性状的杂合子,逐代自交3次,F3
常熟精艺皮件尚湖分厂在哪里啊,我有事要去这
鸡冠山怎么去啊,有知道地址的么
北京大学招文科生还是理科生
学音乐的人有多少有绝对音准
馨广源快餐地址在什么地方,想过去办事
一个各右边一个住是什么字
纯瘦腊肉的热量
状语能作插入语吗? 看下面句子 Bill went bac
太空沙是怎么做的,跪求技术大神指导。想生产
推荐资讯
荣耀7可以升7.0么
磁县图片社港台婚纱影楼地址在什么地方,想过
vue.js中,computed计算属性是否只有一个gett
想开烧烤店怎样能学到变态辣鸡翅配方
有没有什么第三方相机软件,在拍照的时候就可
比较是理解和思维的基石,也是地理学习的重要
合肥到山东济宁市金乡怎么走啊?急!!!!!
“父母经常把复活节彩蛋藏在家里或草丛里让孩
凌渡1.4t豪华 是下雨时自动关闭车窗吗
彼此什么心的四字成语
电影《龙的心》里面的女主角珍妮是谁扮演的
富贵子室内怎么养护
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?