微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 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,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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐