<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

一个绝对经典的在VB中操作.ini文件的通用类源代码

(有需要源程序的,请在留言中留下电子邮件地址)

程序界面:

源程序:

classiniFile.cls的内容:

Option Explicit

'--------classiniFile.cls  代码----------------
'这里定义了一个classiniFile类
'一个绝对经典的在VB中操作.ini文件的通用类源代码
'程序编写:中国青岛·许家国
'    2002.6.16
'E-Mail: goj2000@163.com
'HomePage: http://www.gojclub.com
'

'Private  member  that  holds  a  reference  to
'the  path  of  our  ini  file

Private strini As String

'Windows  API  Declares
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 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 Function MakePath(ByVal strDrv As String, ByVal strDir As String) As String

    '  Makes  an  ini  file:  Guarantees  a  sub  dir
    Do While Right$(strDrv, 1) = ""
          strDrv = Left$(strDrv, Len(strDrv) - 1)
    Loop
   
    Do While Left$(strDir, 1) = ""
          strDir = Mid$(strDir, 2)
    Loop
   
    '  Return  the  path
    MakePath = strDrv & "" & strDir
End Function

Private Sub Createini(strDrv As String, strDir As String)
    '  Make  a  new  ini  file
    strini = MakePath(strDrv, strDir)
End Sub

Public Sub WriteiniKey(strSection As String, strKey As String, strValue As String)
    '  Write  to  strini
    WritePrivateProfileString strSection, strKey, strValue, strini
End Sub

Public Function GetiniKey(strSection As String, strKey As String) As String
    Dim strTmp As String
    Dim lngRet As String
    Dim I As Integer
    Dim strTmp2 As String
   
    strTmp = String$(1024, Chr(32))
    lngRet = GetPrivateProfileString(strSection, strKey, "", strTmp, Len(strTmp), strini)
    strTmp = Trim(strTmp)
    strTmp2 = ""
    For I = 1 To Len(strTmp)
        If Asc(Mid(strTmp, I, 1)) <> 0 Then
            strTmp2 = strTmp2 + Mid(strTmp, I, 1)
        End If
    Next I
    strTmp = strTmp2
   
    GetiniKey = strTmp
End Function

Public Property Let iniFileName(ByVal New_iniPath As String)
    '  Sets  the  new  ini  path
    strini = New_iniPath
End Property

Public Property Get iniFileName() As String
    '  Returns  the  current  ini  path
    iniFileName = strini
End Property

'***************************************清除KeyWord"键"(Sub)***********************************************
Public Function DeliniKey(ByVal SectionName As String, ByVal KeyWord As String)
    Dim RetVal As Integer
    RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, strini)
End Function

'如果是清除section就少写一个Key多一个""。
'**************************************清除 Section"段"(Sub)***********************************************
Public Function DeliniSec(ByVal SectionName As String)      '清除section
    Dim RetVal As Integer
    RetVal = WritePrivateProfileString(SectionName, 0&, "", strini)
End Function

 

Form1中的内容:

Option Explicit

'一个绝对经典的在VB中操作.ini文件的通用类源代码示例程序
'程序编写:中国青岛·许家国
'    2002.6.16
'E-Mail: goj2000@163.com
'HomePage: http://www.gojclub.com

'定义一个.ini类型的变量
Dim Demoini As New classiniFile

Private Sub Form_Load()
    '对控件进行初始化
    Text1.Text = "测试一下"
    List1.Clear
   
    '定义.ini文件名,并写入一些初始数据
    Demoini.iniFileName = App.Path & "demoini.ini"
    Demoini.WriteiniKey "系统", "启动路径", App.Path
    Demoini.WriteiniKey "系统", "可执行程序文件名", App.EXEName
   
    '显示保存到.ini文件中的数据
    Call CmdRead_Click
End Sub

'退出程序
Private Sub CmdExit_Click()
    Unload Me
End Sub

'读取.ini文件中已经存在的数据并显示出来
Private Sub CmdRead_Click()
    Dim TestStr As String
   
    List1.Clear
    TestStr = Demoini.GetiniKey("系统", "启动路径")
    List1.AddItem "系统 - 启动路径: " & TestStr
    TestStr = Demoini.GetiniKey("系统", "可执行程序文件名")
    List1.AddItem "系统 - 可执行程序文件名: " & TestStr
    TestStr = Demoini.GetiniKey("用户自定义", "用户数据")
    List1.AddItem "用户自定义 - 用户数据: " & TestStr
End Sub

'保存用户自定义数据到.ini文件
Private Sub CmdSave_Click()
    Demoini.WriteiniKey "用户自定义", "用户数据", Text1.Text
   
    '显示保存到.ini文件中的数据
    Call CmdRead_Click
End Sub

'清除用户自定义段和段中数据
Private Sub CmdDelete_Click()
    Demoini.DeliniKey "用户自定义", "用户数据"
    Demoini.DeliniSec "用户自定义"
   
    '显示保存到.ini文件中的数据
    Call CmdRead_Click
End Sub

 

 

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐