command1.Caption= "write"
command2.Caption= "read"
command3.Caption= "End"2、代码部分:
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 Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal nDefault As Long, _
ByVal lpFileName As String) As Long
Private Sub Command1_Click()
Dim A As Long
'写信息
'修改ABC.INI文件中各字段中关键字的值
'如果该文件不存在会自动建立,当函数返回值为0时说明修改不成功
A = WritePrivateProfileString("系统时间", "本次操作日期", Str$(Date), App.Path &"\ABC.INI")
A = WritePrivateProfileString("系统时间", "本次操作时间", Str$(Time), App.Path &"\ABC.INI")
A = WritePrivateProfileString("窗体位置及大小", "顶部", Str$(Form1.Top), App.Path &"\ABC.INI")
A = WritePrivateProfileString("窗体位置及大小", "高度", Str$(Form1.Height), App.Path &"\ABC.INI")
A = WritePrivateProfileString("窗体位置及大小", "宽度", Str$(Form1.Width), App.Path &"\ABC.INI")
A = WritePrivateProfileString("窗体位置及大小", "左部", Str$(Form1.Left), App.Path &"\ABC.INI")
If A = 0 Then MsgBox ("写文件时出错")
End Sub
Private Sub Command2_Click()
Dim A As Long
Dim T As String
Dim x As Integer
'读取信息
Form1.Cls 'Form1清屏
T = Space$(1000) '事先定义读取值的字串宽度
'读取ABC.INI文件中TIP字段中START的值并打印出来
'当函数返回值为0时说明读取数据出错
A = GetPrivateProfileString("系统时间", "本次操作时间", "", T, 1000, App.Path &"\ABC.INI")
Print Left$(T, Len(Trim$(T)) - 1)
If A = 0 Then MsgBox "找不到所需字段": Exit Sub
x = GetPrivateProfileInt("窗体位置及大小", "高度", x, App.Path &"\ABC.INI")
Print "高度=" &x
x = GetPrivateProfileInt("窗体位置及大小", "宽度", x, App.Path &"\ABC.INI")
Print "宽度=" &x
x = GetPrivateProfileInt("窗体位置及大小", "左部", x, App.Path &"\ABC.INI")
Print "左部=" &x
x = GetPrivateProfileInt("窗体位置及大小", "顶部", x, App.Path &"\ABC.INI")
Print "顶部=" &x
If A = 0 Then MsgBox "找不到所需字段": Exit Sub
A = GetPrivateProfileString("系统时间", "本次操作日期", "", T, 1000, App.Path &"\ABC.INI")
Print Left$(T, Len(Trim$(T)) - 1)
End SubPrivate Sub Command3_Click()
Call Command1_Click
End
End SubPrivate Sub Form_Load()
Dim A As Long
Dim T As String
Dim x As Integer
'读取信息
x = 0
T = Space$(1000) '事先定义读取值的字串宽度
'读取ABC.INI文件中TIP字段中START的值并打印出来
'当函数返回值为0时说明读取数据出错
x = GetPrivateProfileInt("窗体位置及大小", "高度", x, App.Path &"\ABC.INI")
Form1.Height = x
x = GetPrivateProfileInt("窗体位置及大小", "宽度", x, App.Path &"\ABC.INI")
Form1.Width = x
x = GetPrivateProfileInt("窗体位置及大小", "左部", x, App.Path &"\ABC.INI")
Form1.Left = x
x = GetPrivateProfileInt("窗体位置及大小", "顶部", x, App.Path &"\ABC.INI")
Form1.Top = x
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Select Case UnloadMode
Case 0:
Call Command1_Click
Case 1:
Call Command1_Click
Case 2:
Call Command1_Click
End Select
End Sub
二、说明:
注意是指定数据否与函数参数类型相一致
BugTrace :Bug Trace,bug不用解释了吧,trace 痕迹,意思是bug痕迹,就是bug出现时记录的DumpType:dump 转储,倾倒 , Type 类型,这个不好理解,大约是错误类型吧
用这个例子来写好了,比方说这个ini文件保存在C:\1.ini,然后内容为
[BugTrace]
DumpType = 1
那么代码就是这样写
'首先是API的声明:
'读取用的API
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
'写入用的API
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 F As String
F = "C:\1.ini"
dl = WritePrivateProfileString("BugTrace", "DumpType", 写入的变量, F)
End Sub
'读取
Private Sub Command2_Click()
Dim F As String
F = "C:\1.ini"
GetPrivateProfileString "BugTrace", "DumpType", vbNullString, 读取的变量, 255, F
MsgBox 读取的变量 '这句可以省略,用于显示读取到的数字,如前面的例子就是1
End Sub
上面的 写入的变量 和 读取的变量 请自行修改。
可以使用VB调用系统API函数 GetPrivateProfileString 来从ini文件中读取一条或全部数据。
申明方式如下:
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 LonglpApplicationName:欲在其中查找条目的小节名称。这个字串不区分大小写。如设为vbNullString,就在lpReturnedString缓冲区内装载这个ini文件所有小节的列表。
lpKeyName:欲获取的项名或条目名。这个字串不区分大小写。如设为vbNullString,就在lpReturnedString缓冲区内装载指定小节所有项的列表。
lpDefault:指定的条目没有找到时返回的默认值。可设为空("")。
lpReturnedString:指定一个字串缓冲区,长度至少为nSize。
nSize:指定装载到lpReturnedString缓冲区的最大字符数量。
lpFileName:初始化文件的名字。如没有指定一个完整路径名,windows就在Windows目录中查找文件。
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)