Thao tác với tập tin *.ini

Liên hệ QC

levanduyet

Hãy để gió cuốn đi.
Thành viên danh dự
Tham gia
30/5/06
Bài viết
1,798
Được thích
4,704
Giới tính
Nam
Mã:
Option Explicit
'Đây là hằng, tên mặc định của tập tin *.ini 
Private Const [COLOR="Blue"]csConfig_File_Name[/COLOR] = "SetupDB.ini" '


'Khai báo để dùng cho các hàm GetIni và SetIni
'Xin chú ý chỉ sử dụng cho ANSI
Private Declare Function [COLOR="Blue"]GetPrivateProfileString[/COLOR] 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 [COLOR="Blue"]WritePrivateProfileString[/COLOR] Lib "kernel32" _
                                                   Alias "WritePrivateProfileStringA" _
                                                   (ByVal lpApplicationName As String, _
                                                    ByVal lpKeyName As Any, ByVal lpString As Any, _
                                                    ByVal lpFileName As String) As Long
'Như khai báo ở trên nhưng cho chuổi Unicode
Private Declare Function [COLOR="Blue"]GetPrivateProfileStringW[/COLOR] Lib "kernel32" _
                                                  (ByVal lpApplicationName As Long, _
                                                   ByVal lpKeyName As Long, _
                                                   ByVal lpDefault As Long, ByVal lpReturnedString As Long, _
                                                   ByVal nSize As Long, ByVal lpFileName As Long) As Long


'Mặc định khi sử dụng các hàm  API,
'các tham số sẽ được khai báo dạng A (ANSI).
'Muốn sử dụng dạng Unicode (W) thì chúng ta phải
'chuyển hết các khai báo tham số trong hàm đó thành dạng [COLOR="Blue"]Long[/COLOR].
'Nếu muốn truyền tham số dạng chuổi (String), thì dùng thêm hàm [COLOR="Blue"]StrPtr[/COLOR]

'---------------------------------------------------------------------------------------
[B][COLOR="blue"]' CÁC HÀM, THỦ TỤC DÙNG HÀM API GetPrivateProfileString,WritePrivateProfileString
' ĐỂ LẤY VÀ THIẾT LẬP CÁC GIÁ TRỊ TRONG TẬP TIN INI[/COLOR][/B]
'---------------------------------------------------------------------------------------
[COLOR="seagreen"]' the format of the ini file as following:
' Định dạng của nội dung trong tập tin ini như sau:
'[Section_Header1]
'sKey1= Value1
'sKey2= Value2
'[Section_Header2]
'sKey3= Value3
'sKey4= Value4[/COLOR]
'
[B][U]Chú ý:[/U][/B] [COLOR="Blue"]tập tin ini được xem như đặt trong cùng thư mục với tập tin các bạn dùng các đoạn mã này.[/COLOR]

Function [COLOR="Red"]GetIni[/COLOR](sSection As String, sKey As String, Optional sDefault$ = vbNullString) As String
'We can also declare as GetIni(sSection$, sKey$, Optional sDefault$=vbNullString) As String
'If it can not find the Section or Key then it will return vbNullString
    Dim sTemp As String * 256
    Dim nLength As Integer
    Dim sPathFile As String

    sTemp = Space$(255)
    sPathFile = ThisWbPath() & csConfig_File_Name
    nLength = GetPrivateProfileString(sSection, sKey, sDefault, sTemp, 255, sPathFile)
    GetIni = Left$(sTemp, nLength)

End Function

Sub [COLOR="Red"]SetIni[/COLOR](sSection As String, sKey As String, sValue As String)

    Dim iCounter As Integer
    Dim sPathFile As String
    Dim sTemp As String

    sTemp = sValue
    sPathFile = ThisWbPath() & csConfig_File_Name
    'Replace any CR/LF characters with spaces
    For iCounter = 1 To Len(sValue)
        If Mid$(sValue, iCounter, 1) = vbCr Or Mid$(sValue, iCounter, 1) = vbLf Then Mid$(sValue, iCounter) = " "
    Next iCounter
    iCounter = WritePrivateProfileString(sSection, sKey, sTemp, sPathFile)
End Sub
Khi cần lấy chuổi unicode từ tập tin ini, bạn dùng thủ tục sau:

Mã:
'To get the unicode string from the ini file
Function [COLOR="Red"]GetIniUni[/COLOR](sSection$, sKey$, Optional sDefault$ = vbNullString) As String
    Dim Returned$, Copy&
    Dim sPathFile$
    sPathFile = ThisWbPath() & csConfig_File_Name
    Returned = Space(255)
    Copy = GetPrivateProfileStringW(StrPtr(sSection), _
                                    StrPtr(sKey), StrPtr(sDefault), StrPtr(Returned), 255, StrPtr(sPathFile))
    GetIniUni = Left$(Returned, Copy)
End Function

Lê Văn Duyệt
 
Lần chỉnh sửa cuối:
Web KT
Back
Top Bottom