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

VB载入INI或inf配置文件

VB6.0中怎么在本程序目录自动写如一个配置文件,INF的.

就比如,程序一运行就在本目录生成mbk.inf

然后向里面写入

op=1

id=2

cdkey=3

user=123

然后再读取本配置文件,把里面的内容加到程序里面运行?

补充:就比如这是网络上一个QQ密保卡保护器的关键代码(部分):

Private Function GetPassword(sPSWCode As String) As String
Dim a As Long, b As Long
Dim c As String, d As String
Dim sa As String, sb As String
Dim PSW As Variant
d = vbNullString
If Len(sPSWCode) >= 2 Then
c = UCase(Right(sPSWCode, 2))
sa = Left(c, 1)
sb = Right(c, 1)
If Asc(sa) >= 65 And Asc(sa) <= 74 Then
If Asc(sb) >= 48 And Asc(sb) <= 57 Then
a = Asc(sa) - 65
b = Asc(sb) - 48
PSW = Choose(b * 8 + a + 1, _
1, 11, 21, 31, 41, 51, 61, 71, _
2, 12, 22, 32, 42, 52, 62, 72, _
3, 13, 23, 33, 43, 53, 63, 73, _
4, 14, 24, 34, 44, 54, 64, 74, _
5, 15, 25, 35, 45, 55, 65, 75, _
6, 16, 26, 36, 46, 56, 66, 76, _
7, 17, 27, 37, 47, 57, 67, 77, _
8, 18, 28, 38, 48, 58, 68, 78, _
9, 19, 29, 39, 49, 59, 69, 79, _
10, 20, 30, 40, 50, 60, 70, 80)
'依次输入密保卡0-9行的数据(注意:原密保卡的0行在最下边)
d = Right("00" & PSW, 2)
End If
End If
Else
d = vbNullString
End If
GetPassword = d
End Function

我要把其中

1, 11, 21, 31, 41, 51, 61, 71, _
2, 12, 22, 32, 42, 52, 62, 72, _
3, 13, 23, 33, 43, 53, 63, 73, _
4, 14, 24, 34, 44, 54, 64, 74, _
5, 15, 25, 35, 45, 55, 65, 75, _
6, 16, 26, 36, 46, 56, 66, 76, _
7, 17, 27, 37, 47, 57, 67, 77, _
8, 18, 28, 38, 48, 58, 68, 78, _
9, 19, 29, 39, 49, 59, 69, 79, _
10, 20, 30, 40, 50, 60, 70, 80)
这段改为通过配置文件载入的,要怎么做?

答案: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
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()
'读 ini 文件
Dim F As String, nStr As String, nSize As Long, S As Long
F = "d:\optionv5.ini"
F = App.Path & "\optionv5.ini"
nSize = 255: nStr = String(nSize, 0)
dl = GetPrivateProfileString("Option", "FilterUrl", "", nStr, nSize, F)
S = InStr(nStr, Chr(0))
If S > 0 Then nStr = Left(nStr, S - 1)
MsgBox "读出为:" & nStr '字符串 nStr 就是读出的等号后的内容
End Sub
Private Sub Command2_Click()
'写 ini 文件
Dim F As String
F = "C:My.ini"
dl = WritePrivateProfileString("AAA", "BBB", "CCC", F)
If dl <> 0 Then MsgBox "写 ini 文件成功"
End Sub

上一个:VB数据库编程,求助VB高手!
下一个:vb向access插入数据的一些问题?

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