我有一个vb6项目。它是由大四开发的。现在我想更改现有代码。我是vb6的新手。像ini格式的数据库文件。实际上他使用的是Access数据库。我想在这个数据库中创建一些表。请给出任何想法,在访问数据库中打开ini文件或打开ini文件的任何想法。
答案 0 :(得分:0)
哦,我的,这是旧的东西:
何塞的VB提示&花样使用初始化文件
美国着名作家马克吐温(Mark Twain)对死亡的报道进行了解释 of .ini文件被夸大了。
虽然微软宣称注册表是正确的仓库 初始化信息,.ini文件仍有其用途。之间 .ini文件的优点是:
Windows提供了各种用于处理.ini文件的API,包括 GetProfileXXX和WriteProfileXXX函数致力于使用 win.ini和以下阅读和写作私人的功能 初始化文件:
GetPrivateProfileString 从.ini文件中读取字符串。 GetPrivateProfileInt 从.ini文件中读取整数。 WritePrivateProfileString 将字符串写入.ini文件。 WritePrivateProfileInt 将整数写入.ini文件。
但是,假设.ini文件中的所有数据都是纯文本, 实际上没有必要单独编写这些xxxInt版本的代码 功能。使用VB将字符串转换为VB中的Int非常简单 CInt()或Val()函数,所以只有GetPrivateProfileString和 需要WritePrivateProfileString函数。
这两个都是简单的API调用。有一个例外情况 读取返回带有多个值的C字符串的.ini文件 空值。为了解析该字符串,我已经包含了MultiCStringToStringArray 功能
在继续之前,您需要在声明中添加两个声明 某个模块的某个部分。作为一个习惯问题,我将别名作为Win32 API 使用“w32_”以便我可以编写VB包装函数并为其命名 API函数。
以下是声明部分:
Option Explicit
Private Declare Function w32_GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" ( _
ByVal lpAppName 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 w32_WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long
以下是GetPrivateProfileString的代码:
Public Function GetPrivateProfileString( _
psAppName As String, _
psKeyName As String, _
Optional pvsDefault As Variant, _
Optional pvsFileName As Variant) As String
'********************
' Purpose: Get a string from a private .ini file
' Parameters:
' (Input)
' psApplicationName - the Application name
' psKeyName - the key (section) name
' pvsDefault - Default value if key not found (optional)
' pvsFileName - the name of the .ini file
' Returns: The requested value
' Notes:
' If no value is provided for pvsDefault, a zero-length string is used
' The file path defaults to the windows directory if not fully qualified
' If pvsFileName is omitted, win.ini is used
' If vbNullString is passed for psKeyName, the entire section is returned in
' the form of a multi-c-string. Use MultiCStringToStringArray to parse it after appending the
' second null terminator that this function strips. Note that the value returned is all the
' key names and DOES NOT include all the values. This can be used to setup multiple calls for
' the values ala the Reg enumeration functions.
'********************
' call params
Dim lpAppName As String
Dim lpKeyName As String
Dim lpDefault As String
Dim lpReturnedString As String
Dim nSize As Long
Dim lpFileName As String
' results
Dim lResult As Long
Dim sResult As String
sResult = ""
' setup API call params
nSize = 256
lpReturnedString = Space$(nSize)
lpAppName = psAppName
lpKeyName = psKeyName
' check for value in file name
If Not IsMissing(pvsFileName) Then
lpFileName = CStr(pvsFileName)
Else
lpFileName = "win.ini"
End If
' check for value in optional pvsDefault
If Not IsMissing(pvsDefault) Then
lpDefault = CStr(pvsDefault)
Else
lpDefault = ""
End If
' call
' setup loop to retry if result string too short
Do
lResult = w32_GetPrivateProfileString( _
lpAppName, lpKeyName, lpDefault, lpReturnedString, nSize, lpFileName)
' Note: See docs for GetPrivateProfileString API
' the function returns nSize - 1 if a key name is provided but the buffer is too small
' the function returns nSize - 2 if no key name is provided and the buffer is too small
' we test for those specific cases - this method is a bit of hack, but it works.
' the result is that the buffer must be at least three characters longer than the
' longest string(s)
If (lResult = nSize - 1) Or (lResult = nSize - 2) Then
nSize = nSize * 2
lpReturnedString = Space$(nSize)
Else
sResult = Left$(lpReturnedString, lResult)
Exit Do
End If
Loop
GetPrivateProfileString = sResult
End Function
这是WritePrivateProfileString:
Public Function WritePrivateProfileString( _
psApplicationName As String, _
psKeyName As String, _
psValue As String, _
psFileName As String) As Boolean
'********************
' Purpose: Write a string to an ini file
' Parameters: (Input Only)
' psApplicationName - the ini section name
' psKeyName - the ini key name
' psValue - the value to write to the key
' psFileName - the ini file name
' Returns: True if successful
' Notes:
' Path defaults to windows directory if the file name
' is not fully qualified
'********************
Dim lResult As Long
Dim fRV As Boolean
lResult = w32_WritePrivateProfileString( _
psApplicationName, _
psKeyName, _
psValue, _
psFileName)
If lResult <> 0 Then
fRV = True
Else
fRV = False
End If
WritePrivateProfileString = fRV
End Function
最后,这是MultiCStringToStringArray:
Public Sub MultiCStringToStringArray(psMultiCString As String, psaStrings() As String)
'Created: Joe Garrick 01/06/97 9:28 AM
'********************
' Purpose: Convert a multi-string C string to an array of strings
' Parameters:
' (Input)
' psMultiCString - the multiple C string
' (Output)
' psaStrings - returned array of strings
' Notes:
' The original array should be empty and ReDim-able
'********************
Dim iNullPos As Integer
Dim iPrevPos As Integer
Dim iIdx As Integer
' initialize array, setting first element to a zero-length string
iIdx = 0
ReDim psaStrings(0 To iIdx + 1)
psaStrings(iIdx + 1) = ""
Do
' find null char
iNullPos = InStr(iPrevPos + 1, psMultiCString, vbNullChar)
' double null encountered if next pos is old pos + 1
If iNullPos > iPrevPos + 1 Then
' assing to the string array
psaStrings(iIdx) = Mid$(psMultiCString, (iPrevPos + 1), ((iNullPos - 1) - iPrevPos))
iIdx = iIdx + 1
ReDim Preserve psaStrings(0 To iIdx)
iPrevPos = iNullPos
Else
' double null found, remove last (empty) element and exit
ReDim Preserve psaStrings(0 To iIdx - 1)
Exit Do
End If
Loop
End Sub
这就是编码.ini文件的全部内容。
注释
返回页面顶部[返回页面顶部]
|主页|何塞的Visual Basic世界|何塞的VB提示&amp;技巧| | ©1997 Joe Garrick |信息中心| [电子邮件] jgarrick@citilink.com |