vb ini详细操作

只需要实现保存和读取就可以了,其他的用不着。 别把代码弄得贼老长,而且都是多余的。

一、实现方法如下:1、新建一个工程/窗体(窗体取名为ini.frm),在此窗体中添加三个命令按钮控件,分别为command1、command2、command3
command1.Caption= "write"
command2.Caption= "read"
command3.Caption= "End"2、代码部分:
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 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 GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal nDefault As Long, _
ByVal lpFileName As String) As Long
Private Sub Command1_Click()
Dim A As Long
'写信息
'修改ABC.INI文件中各字段中关键字的值
'如果该文件不存在会自动建立,当函数返回值为0时说明修改不成功
A = WritePrivateProfileString("系统时间", "本次操作日期", Str$(Date), App.Path & "\ABC.INI")
A = WritePrivateProfileString("系统时间", "本次操作时间", Str$(Time), App.Path & "\ABC.INI")
A = WritePrivateProfileString("窗体位置及大小", "顶部", Str$(Form1.Top), App.Path & "\ABC.INI")
A = WritePrivateProfileString("窗体位置及大小", "高度", Str$(Form1.Height), App.Path & "\ABC.INI")
A = WritePrivateProfileString("窗体位置及大小", "宽度", Str$(Form1.Width), App.Path & "\ABC.INI")
A = WritePrivateProfileString("窗体位置及大小", "左部", Str$(Form1.Left), App.Path & "\ABC.INI")
If A = 0 Then MsgBox ("写文件时出错")
End Sub
Private Sub Command2_Click()
Dim A As Long
Dim T As String
Dim x As Integer
'读取信息
Form1.Cls 'Form1清屏
T = Space$(1000) '事先定义读取值的字串宽度
'读取ABC.INI文件中TIP字段中START的值并打印出来
'当函数返回值为0时说明读取数据出错
A = GetPrivateProfileString("系统时间", "本次操作时间", "", T, 1000, App.Path & "\ABC.INI")
Print Left$(T, Len(Trim$(T)) - 1)
If A = 0 Then MsgBox "找不到所需字段": Exit Sub
x = GetPrivateProfileInt("窗体位置及大小", "高度", x, App.Path & "\ABC.INI")
Print "高度=" & x
x = GetPrivateProfileInt("窗体位置及大小", "宽度", x, App.Path & "\ABC.INI")
Print "宽度=" & x
x = GetPrivateProfileInt("窗体位置及大小", "左部", x, App.Path & "\ABC.INI")
Print "左部=" & x
x = GetPrivateProfileInt("窗体位置及大小", "顶部", x, App.Path & "\ABC.INI")
Print "顶部=" & x
If A = 0 Then MsgBox "找不到所需字段": Exit Sub
A = GetPrivateProfileString("系统时间", "本次操作日期", "", T, 1000, App.Path & "\ABC.INI")
Print Left$(T, Len(Trim$(T)) - 1)
End SubPrivate Sub Command3_Click()
Call Command1_Click
End
End SubPrivate Sub Form_Load()
Dim A As Long
Dim T As String
Dim x As Integer
'读取信息
x = 0
T = Space$(1000) '事先定义读取值的字串宽度
'读取ABC.INI文件中TIP字段中START的值并打印出来
'当函数返回值为0时说明读取数据出错
x = GetPrivateProfileInt("窗体位置及大小", "高度", x, App.Path & "\ABC.INI")
Form1.Height = x
x = GetPrivateProfileInt("窗体位置及大小", "宽度", x, App.Path & "\ABC.INI")
Form1.Width = x
x = GetPrivateProfileInt("窗体位置及大小", "左部", x, App.Path & "\ABC.INI")
Form1.Left = x
x = GetPrivateProfileInt("窗体位置及大小", "顶部", x, App.Path & "\ABC.INI")
Form1.Top = x
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Select Case UnloadMode
Case 0:
Call Command1_Click
Case 1:
Call Command1_Click
Case 2:
Call Command1_Click
End Select
End Sub
二、说明:
注意是指定数据否与函数参数类型相一致
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-14
1.定义一些变量Public IniFileName As String, vbNullString As String, maxSize As Long, section1 As String, section2 As String2.初始话这些变量Public Function initial()
IniFileName = App.Path & "" & "config.ini"
vbNullString = ""
maxSize = 255
section1 = "basics"
section2 = "others"
temp_str = String(255, 0) '建立缓冲区
End Function3.声明INI函数Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal nDefault As Long, _
ByVal lpFileName As String) As LongPublic 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 LongPublic Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long4.调用函数dim source as stringGetPrivateProfileString section1, "source", vbNullString, temp_str, maxSize, IniFileName
source = Mid(temp_str, 0, 5) '缓冲区长度249,直接去匹配是不可以的,需要首先提取出有效字符xpos.Text = GetPrivateProfileInt(section1, "xpos", 1, IniFileName) dim a as interger
a = GetPrivateProfileInt(section1, "auto", 1, IniFileName) WritePrivateProfileString section1, "auto", "0", IniFileName 下一个例子 Option ExplicitPrivate 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 LongPrivate Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" ( _
ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, _
ByVal lpFileName As String) As LongPrivate Function GetIni(ByVal ApplicationName As String, ByVal KeyName As String, ByVal FileName As String) As String
Dim Buff As String, TmpStr As String
Buff = String(1024, 0)Call GetPrivateProfileString(ApplicationName, KeyName, "", Buff, Len(Buff) + 1, FileName)If Asc(Mid(Buff, 1, 1)) = 0 Then GetIni = "": Exit FunctionDim I As Integer
For I = 2 To Len(Buff)
If Asc(Mid(Buff, I, 1)) = 0 Then TmpStr = Mid(Buff, 1, I - 1)
Next
If TmpStr = "" Then TmpStr = BuffGetIni = TmpStr
End FunctionPrivate Function PutIni(ByVal ApplicationName As String, ByVal KeyName As String, ByVal Str As String, ByVal FileName As String) As Long
WritePrivateProfileString ApplicationName, KeyName, Str, FileName
End FunctionPrivate Function DelIni(ByVal ApplicationName As String, ByVal KeyName As String, ByVal FileName As String) As Long
WritePrivateProfileString ApplicationName, KeyName, 0&, FileName
End FunctionPrivate Sub Form_Load()
Shell (GetIni("lujing", "pa", "INI文件的路径及名称.ini") & "\" & GetIni("lujing", "pa1", "INI文件的路径及名称.ini"))
End Sub
再次讲解 这是API函数,使用前必须在公共部分定义,可以建公共模块,也可以在窗体最上面写.代码:Public Declare 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 lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long'然后GetPrivateProfileString WritePrivateProfileString 这两个函数就可以用了.'读取和写入代码如下
Private Sub SetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String)
Dim res%
res% = WritePrivateProfileString(SectionName, KeyWord, ValStr, "C:\123.ini")
End Sub
Private Function GetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefString As String) As String
Dim llLen As Long
Dim ResultString As String
ResultString = Space(100)
llLen = GetPrivateProfileString(SectionName, KeyWord, DefString, ResultString, 100, "C:\123.ini")
GetIniS = Mid(ResultString, 1, llLen)
End Function
第2个回答  2013-09-14
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString 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 lpFileName As String) As Long
Private Sub Command1_Click()
'读 ini 文件
Dim F As String, nStr As String, nSize As Long, S As Long
F = "d:\optionv5.ini"
F = App.Path & "\optionv5.ini"
nSize = 255: nStr = String(nSize, 0)
dl = GetPrivateProfileString("Option", "FilterUrl", "", nStr, nSize, F)
S = InStr(nStr, Chr(0))
If S > 0 Then nStr = Left(nStr, S - 1)
MsgBox "读出为:" & nStr '字符串 nStr 就是读出的等号后的内容
End Sub
Private Sub Command2_Click()
'写 ini 文件
Dim F As String
F = "C:My.ini"
dl = WritePrivateProfileString("Option", "FilterUrl", "写入内容", F)
If dl <> 0 Then MsgBox "写 ini 文件成功"
End SubGetPrivateProfileString("Option", "FilterUrl", "", nStr, nSize, F) ini文件[Option]FilterUrl=62447210
第3个回答  2013-09-14
要调用api函数的,声明就很长,短了行吗,长的有,短的没有