大家来帮我看下这个VB读写ini文件vb的类模块块

实例代码如下:Imports SystemImports System.TextImports System.Runtime.InteropServicesNamespace Lob_ini  Public Class cIni    Private ls_IniFilename As String    Private li_BufferLen As Integer = 256    ''' &summary&    ''' cINI Constructor    ''' &/summary&    Public Sub New(ByVal pIniFilename As String)      MyBase.New()      ls_IniFilename = pIniFilename    End Sub    ''' &summary&    ''' INI filename (If no path is specifyed the function will look with-in the Windows directory for the file)    ''' &/summary&    Public Property IniFile() As String      Get        Return      End Get      Set(ByVal value As String)        ls_IniFilename = value      End Set    End Property    ''' &summary&    ''' Max return length when reading data (Max: 32767)    ''' &/summary&    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 WritePrivateProfileString Lib "kernel32" (ByVal pSection As String, ByVal pKey As String, ByVal pValue As String, ByVal pFile As String) As Integer    Private Declare Function WritePrivateProfileStruct 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    ''' &summary&    ''' Read value from INI File    ''' &/summary&    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    ''' &summary&    ''' Read value from INI File, default = ""    ''' &/summary&     Public Overloads Function ReadValue(ByVal pSection As String, ByVal pKey As String) As String      Return z_GetString(pSection, pKey, "")    End Function    ''' &summary&    ''' Write value to INI File    ''' &/summary&    Public Sub WriteValue(ByVal pSection As String, ByVal pKey As String, ByVal pValue As String)      WritePrivateProfileString(pSection, pKey, pValue, Me.ls_IniFilename)    End Sub    ''' &summary&    ''' Remove value from INI File    ''' &/summary&    Public Sub RemoveValue(ByVal pSection As String, ByVal pKey As String)       WritePrivateProfileString(pSection, pKey, Nothing, Me.ls_IniFilename)    End Sub    ''' &summary&    ''' Read values in a section from INI File    ''' &/summary&    Public Sub ReadValues(ByVal pSection As String, ByRef pValues As Array)      pValues = z_GetString(pSection, Nothing, Nothing).Split(CType(ChrW(0), Char))    End Sub    ''' &summary&    ''' Read sections from INI File    ''' &/summary&    Public Sub ReadSections(ByRef pSections As Array)      pSections = z_GetString(Nothing, Nothing, Nothing).Split(CType(ChrW(0), Char))    End Sub    ''' &summary&    ''' Remove section from INI File    ''' &/summary&    Public Sub RemoveSection(ByVal pSection As String)首页 1
【】【】【】【】
ISBN编号:&8
出版时间:&2013-3
出版社:&中国人事出版社
定价:¥45 优惠价:¥45&&ISBN编号:&9
出版时间:&2013-4
出版社:&中国人事出版社
定价:¥45 优惠价:¥45&&
????????????
????????????
         Copyright ©
() All Rights Reservedini文件读写_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
评价文档:
ini文件读写
上传于||文档简介
&&v​b​中​i​n​i​文​件​读​写
大小:21.72KB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢VB怎样读写INI文件?
VB怎样读写INI文件?
08-10-04 &
INI 文件就是 Windows 中常见的以 .ini 为扩展名的文件,其内部格式和各部分的名称如下:[Section1]Key1=Value1Key2=Value2Key3=Vlaue3[Section2]Key1=Value1Key2=Value5Key4=Value4Key5=......     INI 文件中分若干个段 (Section),每个段中有若干个键 (Key) 值 (Value) 对。一个键值对保存一个信息;段则将属性类似的一些键值对组织在一起。同一个段中不能出现两次以上同样的键,但不同的段中可以出现相同的键。     现在明白了吗?要是还不明白,就到 Windows 里找两个 INI 文件看看,文本编辑器就可以打开的。明白了 INI 文件就要开始学习怎样在 VB 中读写 INI 了。     VB 读写 INI 文件,难吗?不难,因为 Windows 已经为我们做好了一切,我们只需要调用它的 API 函数就可以了。为了读写 INI 文件,我们可能用到以下 API 函数:     GetPrivateProfileInt     GetPrivateProfileString     WritePrivateProfileString     其中 WritePrivateProfileString 是用来向 INI 文件写信息的,而GetPrivateProfileInt 和 GetPrivateProfileString 则是用来从 INI 文件中读信息的,前者用于读取整型数据,后者则用于读取字符串型数据。     上述三个 API 函数在 VB 中的申明和说明如下:Private Declare Function GetPrivateProfileInt Lib &kernel32& _Alias &GetPrivateProfileIntA& ( _        ' 返回所读取的长整型值     ByVal lpApplicationName As String, _     ' 要读取的段 (Section) 名称     ByVal lpKeyName As String, _             ' 要读取的的键 (Key) 名称     ByVal nDefault As Long, _                ' 指定默认值,如果读取时出错,则返回该值     ByVal lpFileName As String) As Long      ' 指定要读的 INI 文件名Private Declare Function GetPrivateProfileString Lib &kernel32& _Alias &GetPrivateProfileStringA& ( _     ' 返回所读取的字符串值的真实长度     ByVal lpApplicationName As String, _     ' 要读取的段 (Section) 名称     ByVal lpKeyName As Any, _                ' 要读取的的键 (Key) 名称     ByVal lpDefault As String, _             ' 指定默认值,如果读取时出错,则返回该值     ByVal lpReturnedString As String, _      ' 指定接收返回值的字符串变量     ByVal nSize As Long, _                   ' 指定允许字符串值的最大长度     ByVal lpFileName As String) As Long      ' 指定要读的 INI 文件名Private Declare Function WritePrivateProfileString Lib &kernel32& _Alias &WritePrivateProfileStringA& ( _   ' 如果成功返回非 0 值,失败返回 0     ByVal lpApplicationName As String, _     ' 要写入的段 (Section) 名称     ByVal lpKeyName As Any, _                ' 要写入的的键 (Key) 名称     ByVal lpString As Any, _                 ' 要写入的值 (Value),以字符串表示     ByVal lpFileName As String) As Long      ' 指定要写的 INI 文件名     我们的目的是要在 VB 中写一个读写 INI 文件的类,所以在 VB 中新建一个工程,并添加一个类模块 (Class Module),命令类为 CIniFile,并且将上面的申明添加到类模块中。然后开始为类添加属性和方法。     从上面的注释说明中,我们可以知道,每次调三个 API 之一都需要指定 INI 文件名。而在我们的 CIniFile 的每一个实例中,应该始终访问同一个 INI 文件,所以属性之一就是文件名:     Private IniFileName As String     另外,调用 API 的过程中可能会出现错误,那么 CIniFile 应该能提供错误信息,所以定义一个保存错误信息的变量作为 CIniFile 的第二个属性     Public ErrorMsg As String     由于访问什么段、什么键以及写入什么值都可以通过参数的形式传递给方法,而获取的值也都可以通过方法的返回值得以,所以不再需要其它属性了。不过在定义方法之前还需要对属性进行初始化:Private Sub Class_Initialize()     IniFileName = vbNullString     ErrorMsg = vbNullStringEnd Sub     为了指定 INI 文件名给 CIniFile,需要定义一个方法:Public Sub SpecifyIni(FilePathName)     IniFileName = Trim(FilePathName)End Sub     在每次读写值之前还需要先判断是否已经指定了 INI 文件名,不然读什么写什么啊?Private Function NoIniFile() As Boolean     NoIniFile = True     If IniFileName = vbNullString Then         ErrorMsg = &没有指定 INI 文件&         Exit Function     End If     ErrorMsg = vbNullString     NoIniFile = FalseEnd Function     准备工作完成,现在才是重头戏,读写 INI 文件。似乎“写”要简单一些,就先“写”吧:Public Function WriteString(Section As String, key As String, Value AsString) As Boolean     WriteString = False     If NoIniFile() Then         Exit Function     End If     If WritePrivateProfileString(Section, key, Value, IniFileName) = 0Then         ErrorMsg = &写入失败&         Exit Function     End If     WriteString = TrueEnd Function     该方法在 INI 文件中写入一个键值,成功返回 True,失败返回 False。根据WritePrivateProfileString 的需要,除了文件名这一参数不用提供之外,需要提供段名、键名和值三个参数,而且这三个参数当然来自用户。而WritePrivateProfileString 是通过返回值是否为 0 来判断是否成功的,所以可以通过判断 WritePrivateProfileString 的返回值是否非 0 来返回 True 或 False。     而读 INI 就要稍稍麻烦一点了,两个读取 INI 文件的的函数中,读取字符串那个虽然参数多些,但实现起来却更简单,所以,先写这个:Public Function ReadString(Section As String, key As String, Size AsLong) As String     Dim ReturnStr As String     Dim ReturnLng As Long     ReadString = vbNullString     If NoIniFile() Then         Exit Function     End If     ReturnStr = Space(Size)     ReturnLng = GetPrivateProfileString(Section, key, vbNullString,ReturnStr, Size, IniFileName)     ReadString = Left(ReturnStr, ReturnLng)End Function     这个方法在 INI 文件中读取一个键值,作为字符串返回。如果参数 Size 给定的大小不够,将不能返回完整的值串,但不会有任何提示。     写这个函数的关键在 ReturnStr 的初始化和取值上。VB 中是不需要对字符串进行初始化的,也不需要分配空间。但是这里如果不将它初始化为一个足够长的字符串,就不能正确返回结果。这和 C 语言的字符串有关,就不多说了。ReturnStr 的取值也需要有趣,要使用 Left() 函数将其截断。如果不截断,取得的结果字符串就会有 Size 那幺长,除了取得的值以外,其余部分都是用空格填充的。其原因与前面一点相同,与 C语言的字符串有关。当然 Left() 函数也可以使用 Trim() 代替,效果是一样的。     最后我们不得不面对这个最麻烦的 ReadInt 方法了。它为什么麻烦呢?看看现在的函数定义就知道了:Public Function ReadInt(Section As String, key As String) As Long     Dim ReturnLng As Long     ReadInt = 0     ReturnLng = GetPrivateProfileInt(Section, key, 0, IniFileName)     If ReturnLng = 0 Then         ReturnLng = GetPrivateProfileInt(Section, key, 1, IniFileName)         If ReturnLng = 1 Then             ErrorMsg = &不能读取&             Exit Function         End If     End If     ReadInt = ReturnLngEnd Function     这个方法在 INI 文件中读取一个整数值,失败时返回 0。考虑到某些键的值也可能为 0,故应结合 ErrorMsg 判断是否成功。     这个方法中调用了两次 GetPrivateProfileInt,为什么要这样呢?因为GetPrivateProfileInt 如果成功则返回取得的值,如果不成功则返回给定的默认值。这样就会出现一种情况:如果我给的默认值是 0,GetPrivateProfileInt 函数取得的值也是 0,那么它是成功还是失败呢?同样,如果我给的默认值是1,GetPrivateProfileInt 函数取得的值也是 1,那就是成功还是失败呢?既然一次取值无法判断,那就多取一次,第一次设定默认值为 0,第二次设定默认值为 1,INI 文件的中值不会跟着我的默认值变吧?!虽然这样麻烦一些,但毕竟把问题解决了。     自此,我们终于把一个 CIniFile 写完了——现在读写 INI 文件再也不需要像写CIniFile 一样考虑许多东西了,CIniFile 已经帮我们做了
请登录后再发表评论!vb6读取ini文件后的字符串无法连接 - 开源中国社区
当前访客身份:游客 [
当前位置:
文件目录下有一个a.ini文件 内容如下 [web] a=baidu
vb6模块代码: Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
vb6代码如下 Dim Ret As String Dim buff As String Ret = GetPrivateProfileString("web", "a", "a", buff, 256, App.Path & "\a.ini") Text1.Text = buff + "aaaa"
为什么text1里显示的不是baiduaaaa,而是baidu?
我想text1显示的是baiduaaaa,代码怎么改呢?
共有0个答案
更多开发者职位上
有什么技术问题吗?
vbphp爱...的其它问题
类似的话题VB 二进制块读写类模块应用实例 包括一个文件拷贝和一个文件二进制比较的例子
VB 二进制块读写类模块应用实例 包括一个文件拷贝和一个文件二进制比较的例子,VB 二进制块读写类模块应用实例,包括一个文件拷贝和一个文件二进制比较的例子。
文件拷贝实例:(Text1存放源文件位置,Text2存放目标文件位置)------------------------------------------------------------------------Private Sub Command3_Click()Const BUFFER_SIZE = 40960 * 2 '规定缓冲区大小Dim nActual As LongDim aBuf(0 To BUFFER_SIZE - 1) As Byte '分配缓冲区Dim tmr As Single '计时变量Dim lFileLen As Long '纪录文件长度Dim iFileNum As Integer '记录文件号Dim k As Longtmr = TimerIf Not m_cFileRead.OpenBinary(Text1.Text) Then MsgBox &打开文件失败!& & Text1.TextIf Not m_cFileWrite.OpenBinary(Text2.Text) Then MsgBox &打开文件失败!& & Text2.Text'记下文件长度和打开文件所用的文件号,是为了优化性能。lFileLen = m_cFileRead.FileLengthiFileNum = m_cFileRead.FileNumberk = 0Do k = k + 1 If k = 10 Then k = 0 'pb1是进度条控件。 pb1.Value = 100 * (Seek(iFileNum) / lFileLen) '显示百分比 DoEvents End If '读块,传给它缓冲区指针和期望读取的字节数,返回实际读取的字节数, nActual = m_cFileRead.ReadBlock(VarPtr(aBuf(0)), BUFFER_SIZE) '写块,传给它缓冲区指针和期望写入的字节数。 m_cFileWrite.WriteBlock VarPtr(aBuf(0)), nActualLoop Until nActual < BUFFER_SIZE '当实际读取字节数小于缓冲区大小的时候,就不需要再读啦,已读完啦m_cFileRead.CloseFile '关文件m_cFileWrite.CloseFileMsgBox &OK! total time:& & Timer - tmrEnd Sub---------------------------------------------------------------------------------------------------------------------------------------二进制文件比较实例(Text1存放第一个文件的位置,Text2存放第二个文件的位置)----------------------------Private Sub Command2_Click()Const BUFFER_SIZE As Long = 4096 * 2 '规定缓冲区大小Dim cfRead1 As New CFileReadDim cfread2 As New CFileReadDim aBuf1(0 To BUFFER_SIZE - 1) As Byte '分配缓冲区Dim aBuf2(0 To BUFFER_SIZE - 1) As ByteDim nActual As LongDim i As Long, lCurPos As LongDim tmr As SingleDim k As Longtmr = TimerIf Not cfRead1.OpenBinary(Text1.Text) Then MsgBox &open file failed:& & Text1.TextIf Not cfread2.OpenBinary(Text2.Text) Then MsgBox &open file failed:& & Text2.TextIf cfRead1.FileLength
cfread2.FileLength Then MsgBox &文件不一样长&lCurPos = 1 '当前待比较字节的位置,虚拟位置,不同于文件的读写指针。k = 0Do '读块,传给它缓冲区指针和期望读取的字节数,返回实际读取的字节数, nActual = cfRead1.ReadBlock(VarPtr(aBuf1(0)), BUFFER_SIZE) '读第2个文件的数据块 nActual = cfread2.ReadBlock(VarPtr(aBuf2(0)), BUFFER_SIZE) For i = 0 To nActual - 1 '比较缓冲区 If aBuf1(i)
aBuf2(i) Then MsgBox &不同,位置:& & lCurPos: Exit Do lCurPos = lCurPos + 1 '移到下一个待比较字节的位置 Next i k = k + 1 If k = 100 Then k = 0 Cls Print Timer - tmr, & &, (Seek(cfRead1.FileNumber) / 1024) / (Timer - tmr) & & KB/s& End IfLoop Until nActual < BUFFER_SIZEcfRead1.CloseFile '关文件cfread2.CloseFileMsgBox &OK! & & Timer - tmrEnd Sub---------------------------------------------------------------------------------------------------------------------------------------
来源:/n/9086.html
热门搜索:
PS调色教程 怎样给偏色的照片进行修复美白
Excel2010手动求和图解教程
怎么解决磁盘被保护
怎样使用PS去黄打造青绿色海边 PS效果
怎么做西红柿炒鸡蛋?西红柿炒鸡蛋是一道家常快手菜,以鸡蛋、西红柿为主食材,采用传统工艺炒烹制而成。鸡蛋中含有丰富的蛋白质,西红柿具有抗氧化的特点,这两款食材搭配合理,色泽鲜艳,口味宜人,深受大众喜爱。
水果是女性在怀孕期间必不可少的,水果的营养价值很大的满足了准妈妈整个孕期所需的营养,但是也不是什么水果都能吃,而有“果汁之王”的美誉的百香果,是大家深爱的水果之一,很多准妈妈想吃,那么孕妇可以吃百香果吗?
现在由于全球空气中的二氧化碳增多,一年中温度普遍升高,尤其是夏天的时候气温更高,导致了不少人中暑,一旦中暑了应该怎么办?下面就是一些救治中暑的办法,希望可以帮助大家正确的救治
减肥是女性朋友永恒的话题埋线减肥法是通过调节植物神经和内分泌功能来达到减肥目的,其实只要在正规的医院进行是没有什么副作用,但是也不能排除,下面就给大家来介绍埋线减肥副作用有哪些吧。
火影是笔者非常喜欢的一个动漫,同时火影的游戏也有很高的可玩性,对于新手玩家首先要了解火影忍者究极风暴3出招表,下面我就来教大家这款游戏里一些招式,喜欢这个游戏的玩家不妨来看看。
微信的普遍使用也让很多不法分子盗取个人隐私信息,很多人因此想要注销自己的微信号,首先需要告诉大家的是依照腾讯微信使用规定微信账号是无法注销解除的,下面我来教你怎么最大限度包含您的隐私。
清醒时做事,糊涂时读书,大怒时睡觉,无聊时关注图老师为大家准备的精彩内容。下面为大家推荐PS鼠绘衣橱里的怪兽详细教程,无聊中的都看过来。
炎夏烈日炎炎,就在你与阳光亲密接触时,也让紫外线有了可乘之机,有的时候,虽然你认真做了防晒的工作,却依旧没能躲过炙热的阳光,晒伤之后,要立马着手修复晒伤的肌肤,那么晒伤后怎么处理呢?下面就一起来看看吧。
脸上肉嘟嘟的女生和男生们是不是选择发型的时候很伤大脑。没关系,我们这里肯定有一款适合你的发型,本文也是大圆脸一枚,下面分享下我收集多年的圆脸女生男生发型以及学会的圆脸发型修饰技巧。
孕妇能吃西瓜吗?面对炎炎夏暑的到来,西瓜作为夏季最常见的水果之一,已经成了人们必吃的解暑水果,作为特殊人群的孕妈妈们也馋的直流口水了,那么问题来了,孕妇到底可以吃西瓜吗?下文为你详解。
如果您有什么好的建议或者疑问,可以联系我们! QQ群: QQ号: 进群、加好友,备注:图老师 小周老师一一为您解答!让我们一起学习、一起进步 版权所有 (C) 2016 All Rights Reserved. 湘ICP备号
微信公众号

我要回帖

更多关于 vb的类模块 的文章

 

随机推荐