主题:VB 怎么写配置文件
xiang1558
[专家分:0] 发布于 2008-08-11 09:53:00
我想写个连接数据库的配置文件(INI),然后在表单中需要连接数据库时调用,请哪位会的朋友的给个源代码,谢谢!
回复列表 (共3个回复)
沙发
merry05 [专家分:8920] 发布于 2008-08-11 11:02:00
要写配置文件,使用
Public 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
板凳
snyga [专家分:1480] 发布于 2008-08-11 12:53:00
'给你一个整理的 ini 类
Option Explicit
'读取 ini
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
'写入 ini
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 mIniPath As String
'读取 INI 子键值
Public Function GetKey(AppName As String, _
KeyName As String, _
Optional DefKeyValue As String, _
Optional ByVal IniFilePath As String) As String
Dim str1 As String * 32767
Dim Rec As Long
If Len(Trim$(IniFilePath)) > 0 Then Path = IniFilePath
Rec = GetPrivateProfileString(AppName, KeyName, "", str1, Len(str1), mIniPath)
GetKey = IIf(Rec = 0, DefKeyValue, Replace$(str1, Chr$(0), ""))
End Function
'设置 INI 子键值
Public Function SetKey(AppName As String, _
KeyName As String, _
KeyValue As String, _
Optional ByVal IniFilePath As String) As Long
SetKey = WritePrivateProfileString(AppName, KeyName, KeyValue, mIniPath)
End Function
'读取 小节 下所有 子键名
Public Function GetAllKeyName(AppName As String, _
Optional ByVal IniFilePath As String) As String()
Dim str1 As String * 1024
Dim str2 As String
Dim Rec As Long
Dim p() As String
Dim i As Long
Dim k As Long
Rec = GetPrivateProfileString(AppName, 0&, "", str1, Len(str1), mIniPath)
p = Split(str1, Chr$(0))
For i = 0 To UBound(p)
If Len(p(i)) = 0 Then
If i = 0 Then
p = Split("")
Else
ReDim Preserve p(i - 1)
End If
Exit For
End If
Next
GetAllKeyName = p
End Function
'读取所有 小节名
Public Function GetAllAppName(Optional ByVal IniFilePath As String) As String()
Dim str1 As String * 1024
Dim str2 As String
Dim Rec As Long
Dim p() As String
Dim i As Long
Dim k As Long
Rec = GetPrivateProfileString(vbNullString, 0&, "", str1, Len(str1), mIniPath)
p = Split(str1, Chr$(0))
For i = 0 To UBound(p)
If Len(p(i)) = 0 Then
If i = 0 Then
p = Split("")
Else
ReDim Preserve p(i - 1)
End If
Exit For
End If
Next
GetAllAppName = p
End Function
3 楼
snyga [专家分:1480] 发布于 2008-08-11 12:53:00
'删除 INI AppName
Public Function DelAppName(AppName As String, _
Optional ByVal IniFilePath As String) As Long
DelAppName = WritePrivateProfileString(AppName, 0&, 0&, mIniPath)
End Function
'删除 INI KeyName
Public Function DelKeyName(AppName As String, _
KeyName As String, _
Optional ByVal IniFilePath As String) As Long
DelKeyName = WritePrivateProfileString(AppName, KeyName, 0&, mIniPath)
End Function
Public Property Get Path() As String
Path = mIniPath
End Property
Public Property Let Path(IniFilePath As String)
Dim Path As String
If Mid$(IniFilePath, 1, 2) <> ":\" Then
Path = App.Path
If Right$(Path, 1) <> "\" Then Path = Path & "\"
End If
mIniPath = Path & IniFilePath
End Property
我来回复