为了方便用户使用和使系统具有灵活性,大多数Win-dows应用程序将用户所做的选择以及各种变化的系统信息记录在初始化(INI)文件中。因此,当系统的环境发生变化时,可以直接修改INI文件,而无需修改程序。由此可见,INI文件对系统功能是至关重要的。本文将介绍采用VisualBasicforWindows(下称VB)开发Windows应用程序时如何读写INI文件。 INI文件是文本文件,由若干部分(section)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键词(keyword)和一个等号,每个关键词会控制应用程序某个功能的工作方式,等号右边的值(value)指定关键词的操作方式。其一般形式如下: [Section1] KeyWord1=Valuel KeyWord2=Value2 …… [Section2] KeyWord1=Value1 KeyWord2=Value2 …… 其中,如果等号右边无任何内容(即Value为空),那就表示Windows应用程序已为该关键词指定了缺省值,如果在整个文件中找不到某个关键词(或整个一部分),那同样表示为它们指定了缺省值。各个部分所出现的顺序是无关紧要的,在每一个部分里,各个关键词的顺序同样也无关紧要。 读写INI文件通常有两种方式:一是在Windows中用“记事本”(Notepad)对其进行编辑,比较简单,无需赘述;二是由Windows应用程序读写INI文件,通常是应用程序运行时读取INI文件中的信息,退出应用程序时保存用户对运行环境的某些修改。 关键词的值的类型多为字符串或整数型,应分两种情况读写。为了使程序具有可维护性和可移植性,最好把对INI文件的读写封装在一个模块(RWINI.BAS)中,在RWI-NI.BAS中构造GetIniS和GetIniN函数以及SetIniS和Se-tIniN过程,在这些函数和过程中需要使用WindowsAPI的“GetPrivateprofileString”、“GetPrivateProfileInt”和“WritePrivateProfileString”函数。 RWINI.BAS模块的程序代码如下: 在General-Declearation部分中声明使用到的WindowsAPI函数:Public Declare Function GetprivateprofileString Lib“Kernel”(ByVal lpAppName As String,ByVal lpKeyName As _ String,ByVal lpDefault As String,ByVal lpRetrm-String As String,ByVal cbReturnString As Integer, _ ByVal Filename As String)As Integer Public Declare Function GetPrivatePfileInt Lib“Kernel”(ByVal lpAppName As String,ByVal lpKeyName As String, _ ByVal lpDefault As Integer,ByVal Filename As String)As Integer Public Declare Funciton WritePrivateprofileString Lib“Kernel”(ByVal lpApplicationName As String, _ ByVal lpKeyName As String,ByVal lpString As String,ByVal lplFileName As String)As Integer Public Function GetIniS(ByVal SectionName As String,ByValKeyWord As String,ByVal DefString As String)As String Dim ResultString As String*144,Temp As Integer Dim s As String,i As Integer Temp%=GetPrivateProfileString(SectionName,KeyWord,"",ResultString,144,AppProfileName()) ’检索关键词的值 If Temp%>0 Then ’关键词的值不为空 s="" For i=1 To 144 If Asc(Mid$(ResultString,I,1))=0 Then ExitFor Else s=s & Mid$(ResultString,I,1) End If Next Else Temp%=WritePrivateProfilesString(sectionname,KeyWord,DefString,ppProfileName()) ’将缺省值写入INI文件 s=DefString End If GetIniS=s End FunctionPublic Function GetIniN(ByVal SectionName As String,ByVal KeyWord As String,ByVal DefValue As Ineger)As Integer Dim d As Long,s As String d=DefValue GetIniN=GetPrivateProfileInt(SectionName, KeyWord,DefValue,ppProfileName()) If d<>DefValue Then s="" d=WritePrivateProfileString(SectionName, KeyWord,s,AppProfileName()) End If End FunctionPublic Sub SetIniS(ByVal SectionName As String,BtVal KeyWord As String,ByVal ValStr As String) Dim res% res%=WritePrivateprofileString(SectionName,KeyWord,ValStr,AppProfileName()) End Sub Public Sub SetIniN(ByVal SectionName As String,ByVal KeyWord As String,ByVal ValInt As Integer) Dim res%,s$ s$=Str$(ValInt) res%=WriteprivateProfileString(SectionName,KeyWord,s$,AppProfileName()) End SubSectionName为每一部分的标题,KeyWord为关键词,GetIniS和GetIniN中的DefValue为关键词的缺省值,SetIniS和SetIniN的ValStr和ValInt为要写入INI文件的关键词的值。为了能更好地说明如何使用以上函数和过程,下面举两个实例。实例1: 开发应用程序通常要使用数据库和其它一些文件,这些文件的目录(包括路径和文件名)不应在程序中固定,而是保存在INI文件中,程序运行时由INI文件中读入。读入数据库文件的代码如下:Dim Databasename As String Databasename=GetIniS(“数据库”,“职工”,"") If DatabaseName="" Then DatabaseName=InputBox(“请输入数据库《职工》的目录”),App.Title) ’也可通过“文件对话框”进行选择 On Error Resume Next Set db=OpenDatabas(DatabaseName) If Err<>0 Then MsgBox“打开数据库失败!”,MB-ICONSTOP,App.Title:GotoErrorProcessing Else Set IniS“数据库”,“职工”,DatabaseName End If On Error GoTo 0 ……实例2: 为了方便用户操作,有时需要保存用户界面的某些信息,例如窗口的高度和宽度等。装载窗体时,从INI文件中读入窗体高度和宽度,卸载窗体时将窗体当前高度和宽度存入INI文件,代码如下:Private Sub Form1_Load() …… Forml.Height=GetIniN(“窗体1”,“高度”,6000) Form1.Width=GetIniN(“窗体1”,“高度”,4500) EndSub …… Private Sub Form1_Unload() …… SetIniN“窗体1”,“高度”,Me.Height SetIniN“窗体1,”宽度“,Me.Width …… End Sub
VB读写INI文件的四个函数 '文件名SourceDB.ini文件 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 '以下两个函数,读/写ini文件,固定节点setting,in_key为写入/读取的主键 '仅仅针对是非值 'Y:yes,N:no,E:error Public Function GetIniTF(ByVal In_Key As String) As Boolean On Error GoTo GetIniTFErr GetIniTF = True Dim GetStr As String GetStr = VBA.String(128, 0) GetPrivateProfileString "Setting", In_Key, "", GetStr, 256, App.Path & "\SourceDB.ini" GetStr = VBA.Replace(GetStr, VBA.Chr(0), "") If GetStr = "1" Then GetIniTF = True GetStr = "" Else GoTo GetIniTFErr End If Exit Function GetIniTFErr: Err.Clear GetIniTF = False GetStr = "" End FunctionPublic Function WriteIniTF(ByVal In_Key As String, ByVal In_Data As Boolean) As Boolean On Error GoTo WriteIniTFErr WriteIniTF = True If In_Data = True Then WritePrivateProfileString "Setting", In_Key, "1", App.Path & "\SourceDB.ini" Else WritePrivateProfileString "Setting", In_Key, "0", App.Path & "\SourceDB.ini" End If Exit Function WriteIniTFErr: Err.Clear WriteIniTF = False End Function '以下两个函数,读/写ini文件,不固定节点,in_key为写入/读取的主键 '针对字符串值 '空值表示出错 Public Function GetIniStr(ByVal AppName As String, ByVal In_Key As String) As String On Error GoTo GetIniStrErr If VBA.Trim(In_Key) = "" Then GoTo GetIniStrErr End If Dim GetStr As String GetStr = VBA.String(128, 0) GetPrivateProfileString AppName, In_Key, "", GetStr, 256, App.Path & "\SourceDB.ini" GetStr = VBA.Replace(GetStr, VBA.Chr(0), "") If GetStr = "" Then GoTo GetIniStrErr Else GetIniStr = GetStr GetStr = "" End If Exit Function GetIniStrErr: Err.Clear GetIniStr = "" GetStr = "" End FunctionPublic Function WriteIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String) As Boolean On Error GoTo WriteIniStrErr WriteIniStr = True If VBA.Trim(In_Data) = "" Or VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then GoTo WriteIniStrErr Else WritePrivateProfileString AppName, In_Key, In_Data, App.Path & "\SourceDB.ini" End If Exit Function WriteIniStrErr: Err.Clear WriteIniStr = False End Function
你的类型想来是定义错了,因为你把 str来接收返回值, 其实返回值应是 Long 型的数据Dim Ret as long Dim strV as long ''为什么定义strV, 因为 str是VB自带的函数。strV=space$(255) Ret=GetPrivateProfileString("localconn", "lDBServerName", "",strV, 255, App.Path & "\app.ini") strV = Left(strV, InStr(strV, Chr(0)) - 1)strV 才是得到的值,而且要事先初始化。
Long,复制到x缓冲区的字节数量,其中不包括那些NULL中止字符。如x缓冲区不够大,不能容下全部信息,就返回nSize-1
你应该用
msgbox x
str=GetPrivateProfileString("localconn", "lDBServerName", "",x, 255, App.Path & "\app.ini")
x = Left(x, InStr(x, Chr(0)) - 1)X 才是得到的值,而且要事先初始化。
INI文件是文本文件,由若干部分(section)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键词(keyword)和一个等号,每个关键词会控制应用程序某个功能的工作方式,等号右边的值(value)指定关键词的操作方式。其一般形式如下: [Section1]
KeyWord1=Valuel
KeyWord2=Value2
……
[Section2]
KeyWord1=Value1
KeyWord2=Value2
…… 其中,如果等号右边无任何内容(即Value为空),那就表示Windows应用程序已为该关键词指定了缺省值,如果在整个文件中找不到某个关键词(或整个一部分),那同样表示为它们指定了缺省值。各个部分所出现的顺序是无关紧要的,在每一个部分里,各个关键词的顺序同样也无关紧要。 读写INI文件通常有两种方式:一是在Windows中用“记事本”(Notepad)对其进行编辑,比较简单,无需赘述;二是由Windows应用程序读写INI文件,通常是应用程序运行时读取INI文件中的信息,退出应用程序时保存用户对运行环境的某些修改。 关键词的值的类型多为字符串或整数型,应分两种情况读写。为了使程序具有可维护性和可移植性,最好把对INI文件的读写封装在一个模块(RWINI.BAS)中,在RWI-NI.BAS中构造GetIniS和GetIniN函数以及SetIniS和Se-tIniN过程,在这些函数和过程中需要使用WindowsAPI的“GetPrivateprofileString”、“GetPrivateProfileInt”和“WritePrivateProfileString”函数。 RWINI.BAS模块的程序代码如下: 在General-Declearation部分中声明使用到的WindowsAPI函数:Public Declare Function GetprivateprofileString Lib“Kernel”(ByVal lpAppName As String,ByVal lpKeyName As _
String,ByVal lpDefault As String,ByVal lpRetrm-String As String,ByVal cbReturnString As Integer, _
ByVal Filename As String)As Integer
Public Declare Function GetPrivatePfileInt Lib“Kernel”(ByVal lpAppName As String,ByVal lpKeyName As String, _
ByVal lpDefault As Integer,ByVal Filename As String)As Integer
Public Declare Funciton WritePrivateprofileString Lib“Kernel”(ByVal lpApplicationName As String, _
ByVal lpKeyName As String,ByVal lpString As String,ByVal lplFileName As String)As Integer
Public Function GetIniS(ByVal SectionName As String,ByValKeyWord As String,ByVal DefString As String)As String
Dim ResultString As String*144,Temp As Integer
Dim s As String,i As Integer
Temp%=GetPrivateProfileString(SectionName,KeyWord,"",ResultString,144,AppProfileName())
’检索关键词的值
If Temp%>0 Then ’关键词的值不为空
s=""
For i=1 To 144
If Asc(Mid$(ResultString,I,1))=0 Then
ExitFor
Else
s=s & Mid$(ResultString,I,1)
End If
Next
Else
Temp%=WritePrivateProfilesString(sectionname,KeyWord,DefString,ppProfileName())
’将缺省值写入INI文件
s=DefString
End If
GetIniS=s
End FunctionPublic Function GetIniN(ByVal SectionName As String,ByVal KeyWord As String,ByVal DefValue As Ineger)As Integer
Dim d As Long,s As String
d=DefValue
GetIniN=GetPrivateProfileInt(SectionName,
KeyWord,DefValue,ppProfileName())
If d<>DefValue Then
s=""
d=WritePrivateProfileString(SectionName,
KeyWord,s,AppProfileName())
End If
End FunctionPublic Sub SetIniS(ByVal SectionName As String,BtVal KeyWord As String,ByVal ValStr As String)
Dim res%
res%=WritePrivateprofileString(SectionName,KeyWord,ValStr,AppProfileName())
End Sub
Public Sub SetIniN(ByVal SectionName As String,ByVal KeyWord As String,ByVal ValInt As Integer)
Dim res%,s$
s$=Str$(ValInt)
res%=WriteprivateProfileString(SectionName,KeyWord,s$,AppProfileName())
End SubSectionName为每一部分的标题,KeyWord为关键词,GetIniS和GetIniN中的DefValue为关键词的缺省值,SetIniS和SetIniN的ValStr和ValInt为要写入INI文件的关键词的值。为了能更好地说明如何使用以上函数和过程,下面举两个实例。实例1: 开发应用程序通常要使用数据库和其它一些文件,这些文件的目录(包括路径和文件名)不应在程序中固定,而是保存在INI文件中,程序运行时由INI文件中读入。读入数据库文件的代码如下:Dim Databasename As String
Databasename=GetIniS(“数据库”,“职工”,"")
If DatabaseName="" Then DatabaseName=InputBox(“请输入数据库《职工》的目录”),App.Title)
’也可通过“文件对话框”进行选择
On Error Resume Next
Set db=OpenDatabas(DatabaseName)
If Err<>0 Then
MsgBox“打开数据库失败!”,MB-ICONSTOP,App.Title:GotoErrorProcessing
Else
Set IniS“数据库”,“职工”,DatabaseName
End If
On Error GoTo 0
……实例2: 为了方便用户操作,有时需要保存用户界面的某些信息,例如窗口的高度和宽度等。装载窗体时,从INI文件中读入窗体高度和宽度,卸载窗体时将窗体当前高度和宽度存入INI文件,代码如下:Private Sub Form1_Load()
……
Forml.Height=GetIniN(“窗体1”,“高度”,6000)
Form1.Width=GetIniN(“窗体1”,“高度”,4500)
EndSub
……
Private Sub Form1_Unload()
……
SetIniN“窗体1”,“高度”,Me.Height
SetIniN“窗体1,”宽度“,Me.Width
……
End Sub
'文件名SourceDB.ini文件
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
'以下两个函数,读/写ini文件,固定节点setting,in_key为写入/读取的主键
'仅仅针对是非值
'Y:yes,N:no,E:error
Public Function GetIniTF(ByVal In_Key As String) As Boolean
On Error GoTo GetIniTFErr
GetIniTF = True
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString "Setting", In_Key, "", GetStr, 256, App.Path & "\SourceDB.ini"
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "1" Then
GetIniTF = True
GetStr = ""
Else
GoTo GetIniTFErr
End If
Exit Function
GetIniTFErr:
Err.Clear
GetIniTF = False
GetStr = ""
End FunctionPublic Function WriteIniTF(ByVal In_Key As String, ByVal In_Data As Boolean) As Boolean
On Error GoTo WriteIniTFErr
WriteIniTF = True
If In_Data = True Then
WritePrivateProfileString "Setting", In_Key, "1", App.Path & "\SourceDB.ini"
Else
WritePrivateProfileString "Setting", In_Key, "0", App.Path & "\SourceDB.ini"
End If
Exit Function
WriteIniTFErr:
Err.Clear
WriteIniTF = False
End Function
'以下两个函数,读/写ini文件,不固定节点,in_key为写入/读取的主键
'针对字符串值
'空值表示出错
Public Function GetIniStr(ByVal AppName As String, ByVal In_Key As String) As String
On Error GoTo GetIniStrErr
If VBA.Trim(In_Key) = "" Then
GoTo GetIniStrErr
End If
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString AppName, In_Key, "", GetStr, 256, App.Path & "\SourceDB.ini"
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "" Then
GoTo GetIniStrErr
Else
GetIniStr = GetStr
GetStr = ""
End If
Exit Function
GetIniStrErr:
Err.Clear
GetIniStr = ""
GetStr = ""
End FunctionPublic Function WriteIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String) As Boolean
On Error GoTo WriteIniStrErr
WriteIniStr = True
If VBA.Trim(In_Data) = "" Or VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then
GoTo WriteIniStrErr
Else
WritePrivateProfileString AppName, In_Key, In_Data, App.Path & "\SourceDB.ini"
End If
Exit Function
WriteIniStrErr:
Err.Clear
WriteIniStr = False
End Function
其实返回值应是 Long 型的数据Dim Ret as long
Dim strV as long ''为什么定义strV, 因为 str是VB自带的函数。strV=space$(255)
Ret=GetPrivateProfileString("localconn", "lDBServerName", "",strV, 255, App.Path & "\app.ini")
strV = Left(strV, InStr(strV, Chr(0)) - 1)strV 才是得到的值,而且要事先初始化。