当前位置:编程学习 > VB >>

VB怎么读写INI文件

假如现在窗口有10个文本框,怎么将它们的名称和值写到INI文件,下次打开时怎么读取?麻烦给代码,并解释下代码。给高分表谢意!
答案:方法:建立一工程,随便命名

控件 8个command,2个text ,

代码如下:

'API声明

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function GetPrivateProfileString Lib "Kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String, ByVal lpRetunedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "Kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lplFileName As String) As Long

‘变量声明

Private r As Long
Private entry As String
Private iniPath As String

’定义

Function GetFromINI(AppName As String, KeyName As String, FileName As String) As String
   Dim RetStr As String
   RetStr = String(255, Chr(0))
   GetFromINI = Left(RetStr, GetPrivateProfileString(AppName, ByVal KeyName, "", RetStr, Len(RetStr), FileName))
End Function

Private Sub Command1_Click()
    Text1.Text = GetFromINI("zc", "ID", iniPath)
End Sub

Private Sub Command8_Click()
    End
End Sub

Private Sub Command2_Click()
    Text2.Text = GetFromINI("yh", "NAME", iniPath)
End Sub


Private Sub Command4_Click()
    entry$ = Text1.Text
    r = WritePrivateProfileString("zc", "ID", entry, iniPath)
    If r <> 1 Then MsgBox "error!"
End Sub

Private Sub Command5_Click()
    entry = Text2.Text
    r = WritePrivateProfileString("yh", "NAME", entry, iniPath)
    If r <> 1 Then MsgBox "error!"
End Sub

Private Sub Command7_Click()
    Text1.Text = GetFromINI("zc", "ID", iniPath)
    Text2.Text = GetFromINI("yh", "NAME", iniPath)
End Sub

Private Sub Command3_Click()
    Command4_Click
    Command5_Click
End Sub

Private Sub Command6_Click()
    Text1.Text = ""
    Text2.Text = ""
End Sub

Private Sub Form_Load()
    iniPath$ = App.Path + "\GLKJ.ini"
    Command7_Click
End Sub

结束,可以测试了

'读收ini的模块

Option Explicit

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
Public Declare Function WritePrivateProfileString Lib "Kernel32" Alias "WritePrivateProfileStringA" (ByVal AppName As String, ByVal KeyName As String, ByVal keydefault As String, ByVal Filename As String) As Long

Public Function myReadINI(iniFileName, iniSection, iniKey, iniDefault)
'该函数的使用与读注册表类似
'inifilename为INI文件名,inisection为INI文件中的项目,inikey为项目下的键名称,inidefault为默认键值
'If no section (appname), default is first appname
'(若无项目名,默认为初始名称)
'if no key, default is first key
'(若无键名,默认为初始键名)
Dim lpApplicationName As String
Dim lpKeyName As String
Dim lpDefault As String
Dim lpReturnedString As String
Dim nSize As Long
Dim lpFileName As String
Dim retval As Long
Dim Filename As String
Dim intZeroPos As Integer
'判断INI文件是否存在
If Dir(iniFileName) <> "" Then
    lpDefault = Space$(254)
    lpDefault = iniDefault
    lpReturnedString = Space$(254)
    nSize = 254
    lpFileName = iniFileName
    lpApplicationName = iniSection
    lpKeyName = iniKey
    Filename = lpFileName
    retval = GetPrivateProfileString(lpApplicationName, lpKeyName, lpDefault, lpReturnedString, nSize, lpFileName)
   
    '去掉后面的空格

    myReadINI = StripTerminator(lpReturnedString)


End If
End Function

Public Function myWriteINI(iniFileName As String, iniSection As String, iniKey As String, Info As String) As String
'该函数的使用与写注册表类似,可在INI文件中添加或修改项、键、值
'iniFileName为INI文件名,inisection为INI文件中的项目,inikey为项目下的键名称,Info为键值
Dim retval As Long
    retval = WritePrivateProfileString(iniSection, iniKey, Info, iniFileName)
    myWriteINI = LTrim$(Str$(retval))
End Function

Public Sub DelSectionINI(iniFileName As String, iniSection As String)
'该过程可删除INI文件中指定的项
'iniFileName为INI文件名,iniSection为指定的项
'判断INI文件是否存在
If Dir(iniFileName) <> "" Then
    WritePrivateProfileString iniSection, vbNullString, vbNullString, iniFileName
End If

End Sub

Public Sub DelKeyINI(iniFileName As String, iniSection As String, iniKey As String)
'该过程可删除INI文件中指定的键
'iniFileName为INI文件名,iniSection为指定的项,iniKey为指定的键
'判断INI文件是否存在
If Dir(iniFileName) <> "" Then
    WritePrivateProfileString iniSection, iniKey, vbNullString, iniFileName
End If

End Sub

Public Sub DelValueINI(iniFileName As String, iniSection As String, iniKey As String)
'该过程可删除INI文件中指定键的值
'iniFileName为INI文件名,iniSection为指定的项,iniKey为指定的键
'判断INI文件是否存在
If Dir(iniFileName) <> "" Then
    WritePrivateProfileString iniSection, iniKey, "", iniFileName
End If

End Sub

Public Sub DelFileINI(iniFileName As String)
'该过程可删除INI文件
'iniFileName为INI文件名
'判断INI文件是否存在
If Dir(iniFileName) <> "" Then
    Kill iniFileName
End If

End Sub

'-----------------------------------------------------------
' FUNCTION: StripTerminator
'
' Returns a string without any zero terminator.  Typically,
' this was a string returned by a Windows API call.
'去掉用API函数读取的变量的后面的空格
' IN: [strString] - String to remove terminator from
'
' Returns: The value of the string passed in minus any
'          terminating zero.
'-----------------------------------------------------------
'
Function StripTerminator(ByVal strString As String) As String
    Dim intZeroPos As Integer

    intZeroPos = InStr(strString, Chr$(0))
    If intZeroPos > 0 Then
        StripTerminator = Left$(strString, intZeroPos - 1)
    Else
        StripTerminator = strString
    End If
End Function


'模块结束

 

 

Private Sub Form_Load()

'窗体加载时读入数据

text1.text=myReadINI("c:/aaa.ini","值","text1","")

text2.text=myReadINI("c:/aaa.ini","值","text2","")

End Sub

 

 

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

'窗体退出时写ini
myWriteINI "c:/aaa.ini", "值", "text1", Text1.Text
myWriteINI "c:/aaa.ini", "值", "text2", Text2.Text
End Sub

 ????为什么审核不通过???

上一个:求vb代码一段
下一个:vb连接access数据库代码详细解释

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,