加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

VB操作INI文件读写示例

发布时间:2020-12-17 08:19:53 所属栏目:百科 来源:网络整理
导读: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 LongPublic Declare Function
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读