VB操作.ini后缀文件的方法
在窗体放置两个命令按钮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
Tags:
作者:佚名评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论