VB 程序设计

10个成员

VB操作.ini后缀文件的方法

发表于 2016-12-22 3369 次查看
   读文件用到GetPrivateProfileString,写文件需要用到WritePrivateProfileString。
   在窗体放置两个命令按钮Command1与Command2,分别用来执行写操作与读操作。
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 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 Sub Command1_Click()
Dim A As Long
'写信息
'修改ABC.INI文件中TIP字段中START的值为当前系统时间
'如果该文件不存在会自动建立,当函数返回值为0时说明修改不成功
A = WritePrivateProfileString("TIP", "START", Time$, App.Path & "\ABC.INI")
If A = 0 Then MsgBox ("写文件时出错")
End Sub


Private Sub Command2_Click()
Dim A As Long
Dim T As String
'读取信息
T = Space$(1000) '事先定义读取值的字串宽度
'读取ABC.INI文件中TIP字段中START的值并打印出来
'当函数返回值为0时说明读取数据出错
A = GetPrivateProfileString("TIP", "START", "", T, 1000, App.Path & "\ABC.INI")
If A = 0 Then MsgBox "找不到所需字段": Exit Sub
Print Left$(T, Len(Trim$(T)) - 1)
End Sub

 

发表回复
你还没有登录,请先登录注册