当前位置:编程学习 > VB >>

VB读写ini的问题

写:

Private Sub Tuichu()
Dim A As Long
A = WritePrivateProfileString("checkbox", "自动保存", Str$(Check1), App.path & "\config.ini")
A = WritePrivateProfileString("checkbox", "定时拍照", Str$(Check2), App.path & "\config.ini")
A = WritePrivateProfileString("text", "照片路径", Str$(Text2.Text), App.path & "\config.ini")
    If A = 0 Then MsgBox ("写文件时出错")

 

读:

x = 0
    T = Space$(1000) '事先定义读取值的字串宽度
'读取ABC.INI文件中TIP字段中START的值并打印出来
'当函数返回值为0时说明读取数据出错
      x = GetPrivateProfileInt("checkbox", "自动保存", x, App.path & "\config.ini")
      Check1 = x
      x = GetPrivateProfileInt("checkbox", "定时拍照", x, App.path & "\config.ini")
      Check2 = x
       x = GetPrivateProfileInt("text", "照片路径", x, App.path & "\config.ini")
       Text2.Text = x

 

 

以上两条代码哪里错了,为什么保存不了text2的内容进ini??

生产出来的exe,运行后,然后再关闭,总是显示A = WritePrivateProfileString("text", "照片路径", Str$(Text2.Text), App.path & "\config.ini")  这一段类型不匹配。错误代码13

 

 

  谢谢各位大虾指导。

补充:声明我忘了打上来,其实已经有了

追问:可是,我的text2是用来存放路径的,不写进ini,那下次启动程序的时候,路径又要重设了。

答案:首先需要声明API函数,声明后才能使用,所以改成如下就可以写ini了。

Option Explicit
Private 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
Private 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

Private Sub Tuichu()
Dim A As Long
A = WritePrivateProfileString("checkbox", "自动保存", Str$(Check1), App.Path & "\config.ini")
A = WritePrivateProfileString("checkbox", "定时拍照", Str$(Check2), App.Path & "\config.ini")
A = WritePrivateProfileString("text", "照片路径", Str$(Text1.Text), App.Path & "\config.ini")
    If A = 0 Then MsgBox ("写文件时出错")
End Sub

 

上一个:求VB实用函数
下一个:VB结束进程详解

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,