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
Dim B As String
B = App.Path
'写信息
'修改SET.INI文件中MAIN字段中HP的值为当前程序路径
'如果该文件不存在会自动建立,当函数返回值为0时说明修改不成功
A = WritePrivateProfileString("MAIN", "HP", B, App.Path &"\SET.INI")
If A = 0 Then MsgBox ("写文件时出错")
End Sub
Private Sub Command2_Click()
Dim A As Long
Dim T As String
'读取信息
T = Space$(1000) '事先定义读取值的字串宽度
'读取SET.INI文件中TIP字段中MAIN的值并打印出来
'当函数返回值为0时说明读取数据出错
A = GetPrivateProfileString("MAIN", "HP", "", T, 1000, App.Path &"\SET.INI")
If A = 0 Then MsgBox "找不到所需字段": Exit Sub
Print Left$(T, Len(Trim$(T)) - 1)
End Sub
把以下代码拷贝到一个空窗体,窗体建立两个文本框,运行会自动在运行目录下生成ini文件,实现你要的要求。没有加checkbox控件,和文本框原理一样。' 定义API函数
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
' 为方便使用,把两个API函数封装起来
Function ReadIniFile(filename As String, AppName As String, KeyName As String) As String
Dim temp As String * 100
dim n as long
n = GetPrivateProfileString(AppName, KeyName, "", temp, Len(temp), fileName)
ReadIniFile = Mid(temp, 1, n)
End Function
Function WriteIniFile(filename As String, AppName As String, KeyName As String, NewKeyName As String)
dim n as long
n = WritePrivateProfileString(AppName, KeyName, NewKeyName, fileName)
End Function
'下面这里是当窗体加载时,读取ini文件中的值,并放到txtbox里
Private Sub Form_Load()
Dim strIniFileName As String
strIniFileName = App.Path &"\test.ini"
'程序载入时,自动提取两个文本框的值 ( 没有文件返回空)
Text1.Text = ReadIniFile(strIniFileName, "配置信息", "属性一")
Text2.Text = ReadIniFile(strIniFileName, "配置信息", "属性二")
End Sub
Private Sub Form_Unload(Cancel As Integer)
'这里定义窗体关闭时保存到ini,你可以改成点击按钮
Dim strIniFileName As String
strIniFileName = App.Path &"\test.ini"
' 窗体关闭时,自动保存两个文本框的值 (没有ini文件自动建立)
Call WriteIniFile(strIniFileName, "配置信息", "属性一", Text1.Text)
Call WriteIniFile(strIniFileName, "配置信息", "属性二", Text2.Text)
End Sub
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)