VB操作INI文件读写示例

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
Public 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 Const mc_strIniFileName As String = "MyIni.ini"


Public Sub WriteIni(appName As String, keyName As String, valueNew As String)
'
    Dim x As Long
    Dim strIniFile As String
    
    If Right(App.Path, 1) = "\" Then
        strIniFile = App.Path & mc_strIniFileName
    Else
        strIniFile = App.Path & "\" & mc_strIniFileName
    End If
    x = WritePrivateProfileString(appName, keyName, valueNew, strIniFile)
    Debug.Print x
End Sub

Public Function GetIni(appName As String, keyName As String) As String
'
    Dim strDefault As String
    Dim lngBuffLen As Long
    Dim strResu As String
    Dim x As Long
    Dim strIniFile As String
    
    If Right(App.Path, 1) = "\" Then
        strIniFile = App.Path & mc_strIniFileName
    Else
        strIniFile = App.Path & "\" & mc_strIniFileName
    End If
    
    strResu = String(1025, vbNullChar): lngBuffLen = 1025
    strDefault = ""
    x = GetPrivateProfileString(appName, keyName, strDefault, strResu, lngBuffLen, strIniFile)
    Debug.Print x
    Debug.Print strResu
    GetIni = Left(strResu, x)
    
End Function



VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   2085
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   2085
   ScaleWidth      =   4680
   StartUpPosition =   3  '窗口缺省
   Begin VB.TextBox Text3 
      Height          =   495
      Left            =   1560
      TabIndex        =   4
      Top             =   1440
      Width           =   1215
   End
   Begin VB.TextBox Text2 
      Height          =   495
      Left            =   1560
      TabIndex        =   3
      Top             =   720
      Width           =   1215
   End
   Begin VB.TextBox Text1 
      Height          =   495
      Left            =   1560
      TabIndex        =   2
      Top             =   0
      Width           =   1215
   End
   Begin VB.CommandButton Command2 
      Caption         =   "读取键值"
      Height          =   495
      Left            =   3240
      TabIndex        =   1
      Top             =   1440
      Width           =   1215
   End
   Begin VB.CommandButton Command1 
      Caption         =   "写入键值"
      Height          =   495
      Left            =   3240
      TabIndex        =   0
      Top             =   720
      Width           =   1215
   End
   Begin VB.Label Label3 
      Caption         =   "值"
      Height          =   495
      Left            =   120
      TabIndex        =   7
      Top             =   1440
      Width           =   1215
   End
   Begin VB.Label Label2 
      Caption         =   "键名"
      Height          =   495
      Left            =   120
      TabIndex        =   6
      Top             =   720
      Width           =   1215
   End
   Begin VB.Label Label1 
      Caption         =   "段名"
      Height          =   495
      Left            =   120
      TabIndex        =   5
      Top             =   0
      Width           =   1215
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
'
    WriteIni Text1.Text, Text2.Text, Text3.Text
    
End Sub

Private Sub Command2_Click()
'
    Text3.Text = GetIni(Text1.Text, Text2.Text)

End Sub


你可能感兴趣的:(VB)