VB中INI文件操作

首先是一个介绍的文章:

为了方便用户使用和使系统具有灵活性,大多数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 Function

Public 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 Function

Public 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 Sub

SectionName为每一部分的标题,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


INI文件操作经常用到的一些API函数:
  1. Option   Explicit  
  2.   'win.ini  
  3.   Declare   Function   GetProfileInt   Lib   "kernel32"   Alias   "GetProfileIntA"   _  
  4.   (ByVal   lpAppName   As   String,   _  
  5.   ByVal   lpKeyName   As   String,   _  
  6.   ByVal   nDefault   As   Long)   As   Long  
  7.    
  8.   Declare   Function   GetProfileSection   Lib   "kernel32"   Alias   "GetProfileSectionA"   _  
  9.   (ByVal   lpAppName   As   String,   _  
  10.   ByVal   lpReturnedString   As   String,   _  
  11.   ByVal   nSize   As   Long)   As   Long  
  12.    
  13.   Declare   Function   GetProfileString   Lib   "kernel32"   Alias   "GetProfileStringA"   _  
  14.   (ByVal   lpAppName   As   String,   _  
  15.   ByVal   lpKeyName   As   String,   _  
  16.   ByVal   lpDefault   As   String,   _  
  17.   ByVal   lpReturnedString   As   String,   _  
  18.   ByVal   nSize   As   Long)   As   Long  
  19.    
  20.   Declare   Function   WriteProfileSection   Lib   "kernel32"   Alias   "WriteProfileSectionA"   _  
  21.   (ByVal   lpAppName   As   String,   _  
  22.   ByVal   lpString   As   String)   As   Long  
  23.    
  24.   Declare   Function   WriteProfileString   Lib   "kernel32"   Alias   "WriteProfileStringA"   _  
  25.   (ByVal   lpszSection   As   String,   _  
  26.   ByVal   lpszKeyName   As   String,   _  
  27.   ByVal   lpszString   As   String)   As   Long  
  28.    
  29.   '*.ini      
  30.   Declare   Function   GetPrivateProfileInt   Lib   "kernel32"   Alias   "GetPrivateProfileIntA"   _  
  31.   (ByVal   lpApplicationName   As   String,   _  
  32.   ByVal   lpKeyName   As   String,   _  
  33.   ByVal   nDefault   As   Long,   _  
  34.   ByVal   lpFileName   As   String)   As   Long  
  35.    
  36.   Declare   Function   GetPrivateProfileSection   Lib   "kernel32"   Alias   "GetPrivateProfileSectionA"   _  
  37.   (ByVal   lpAppName   As   String,   _  
  38.   ByVal   lpReturnedString   As   String,   _  
  39.   ByVal   nSize   As   Long,   _  
  40.   ByVal   lpFileName   As   String)   As   Long  
  41.    
  42.   Declare   Function   GetPrivateProfileString   Lib   "kernel32"   Alias   "GetPrivateProfileStringA"   _  
  43.   (ByVal   lpApplicationName   As   String,   _  
  44.   ByVal   lpKeyName   As   Any,   _  
  45.   ByVal   lpDefault   As   String,   _  
  46.   ByVal   lpReturnedString   As   String,   _  
  47.   ByVal   nSize   As   Long,   _  
  48.   ByVal   lpFileName   As   String)   As   Long  
  49.    
  50.   Declare   Function   WritePrivateProfileSection   Lib   "kernel32"   Alias   "WritePrivateProfileSectionA"   _  
  51.   (ByVal   lpAppName   As   String,   _  
  52.   ByVal   lpString   As   String,   _  
  53.   ByVal   lpFileName   As   String)   As   Long  
  54.    
  55.   Declare   Function   WritePrivateProfileString   Lib   "kernel32"   Alias   "WritePrivateProfileStringA"   _  
  56.   (ByVal   lpApplicationName   As   String,   _  
  57.   ByVal   lpKeyName   As   Any,   _  
  58.   ByVal   lpString   As   Any,   _  
  59.   ByVal   lpFileName   As   String)   As   Long
网上搜索的一个最简单的读写INI文件的例子
  1. Option   Explicit  
  2.   Private   Declare   Function   WritePrivateProfileString   _  
  3.                   Lib   "kernel32"   Alias   "WritePrivateProfileStringA"   _  
  4.                   (ByVal   lpApplicationName   As   String,   _  
  5.                   ByVal   lpKeyName   As   Any,   _  
  6.                   ByVal   lpString   As   Any,   _  
  7.                   ByVal   lpFileName   As   String)   As   Long  
  8.   Public   Declare   Function   GetPrivateProfileString   _  
  9.                   Lib   "kernel32"   Alias   "GetPrivateProfileStringA"   _  
  10.                   (ByVal   lpApplicationName   As   String,   _  
  11.                   ByVal   lpKeyName   As   Any,   _  
  12.                   ByVal   lpDefault   As   String,   _  
  13.                   ByVal   lpReturnedString   As   String,   _  
  14.                   ByVal   nSize   As   Long,   _  
  15.                   ByVal   lpFileName   As   String)   As   Long  
  16.    
  17.    
  18.   'FileName:Ini文件  
  19.   'PathName:小节名  
  20.   'KeyName:值名  
  21.   'WriteValue:值  
  22.   Public   Function   WriteIni(FileName   As   String,   _  
  23.                   PathName   As   String,   _  
  24.                   KeyName   As   String,   _  
  25.                   WriteValue   As   String)   As   Long  
  26.           Dim   Rc   As   Long  
  27.            
  28.           Rc   =   WritePrivateProfileString(PathName,   KeyName,   WriteValue,   FileName)  
  29.           WriteIni   =   Rc  
  30.            
  31.   End   Function  
  32.    
  33.   'FileName:Ini文件  
  34.   'PathName:小节名  
  35.   'KeyName:值名  
  36.   'BackValue:返回值  
  37.   'Default:默认字符  
  38.   Public   Function   ReadIni(FileName   As   String,   _  
  39.                   PathName   As   String,   _  
  40.                   KeyName   As   String,   _  
  41.                   BackValue   As   String,   _  
  42.                   Optional   Default   As   String   =   "缺省")   As   Long  
  43.           Dim   Rc   As   Long  
  44.           Dim   TempNum   As   String  
  45.           Dim   TempStr   As   String  
  46.            
  47.           TempStr   =   String$(255,   Chr$(0))  
  48.           TempNum   =   255  
  49.            
  50.           Rc   =   GetPrivateProfileString(PathName,   KeyName,   Default,   TempStr,   TempNum,   FileName)  
  51.            
  52.           If   Rc   <>   0   Then  
  53.                   BackValue   =   Left$(TempStr,   TempNum)
  54.           End   If  
  55.            
  56.           ReadIni   =   Rc  
  57.            
  58.   End   Function 
一个可以循环所有Section的例子
  1. Public   File   As   String  
  2.    
  3.   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   Integer,   ByVal   lpFileName   As   String)   As   Integer  
  4.   Private   Declare   Function   WritePrivateProfileString   Lib   "kernel32"   Alias   "WritePrivateProfileStringA"   (ByVal   Appname   As   String,   ByVal   KeyName   As   Any,   ByVal   NewString   As   Any,   ByVal   Filename   As   String)   As   Integer  
  5.    
  6.   Sub   DeleteSection(ByVal   Section   As   String)  
  7.    
  8.   Dim   retval   As   Integer  
  9.    
  10.       retval   =   WritePrivateProfileString(Section,   0&,   "",   File)  
  11.    
  12.   End   Sub  
  13.   Public   Function   SaveSetting(ByVal   Section$,   ByVal   Key$,   ByVal   Value$)  
  14.    
  15.   Dim   retval   As   Integer  
  16.    
  17.       SaveSetting   =   WritePrivateProfileString(Section$,   Key$,   Value$,   File)  
  18.    
  19.   End   Function  
  20.    
  21.   Public   Function   GetSetting(ByVal   Section   As   String,   ByVal   KeyName   As   String)   As   String  
  22.    
  23.   Dim   retval   As   Integer  
  24.   Dim   t   As   String   *   255  
  25.    
  26.       '   Get   the   value  
  27.       retval   =   GetPrivateProfileString(Section,   KeyName,   "unknown   value",   t,   Len(t),   File)  
  28.    
  29.       '   If   there   is   one,   return   it  
  30.       If   retval   >   0   Then  
  31.               GetSetting   =   Left$(t,   retval)  
  32.       Else  
  33.               GetSetting   =   "Unknown   section   or   key"  
  34.       End   If  
  35.    
  36.   End   Function  
  37.    
  38.   //这个可以枚举所有项  
  39.   Public   Function   GetSection(ByVal   Section   As   String,   KeyArray()   As   String)   As   Integer  
  40.    
  41.   Dim   retval   As   Integer  
  42.   '   Allocate   space   for   return   value  
  43.   Dim   t   As   String   *   2500  
  44.   Dim   lastpointer   As   Integer  
  45.   Dim   nullpointer   As   Integer  
  46.   Dim   ArrayCount   As   Integer  
  47.   Dim   keystring   As   String  
  48.        
  49.       ReDim   KeyArray(0)  
  50.        
  51.       '   Get   the   value  
  52.       retval   =   GetPrivateProfileString(Section,   0&,   "",   t,   Len(t),   File)  
  53.        
  54.       '   If   there   is   one,   return   it  
  55.       If   retval   >   0   Then  
  56.               '  
  57.               '   Separate   the   keys   and   store   them   in   the   array  
  58.               nullpointer   =   InStr(t,   Chr$(0))  
  59.               lastpointer   =   1  
  60.               Do   While   (nullpointer   <>   0   And   nullpointer   >   lastpointer   +   1)  
  61.                   '  
  62.                   '   Extract   key   string  
  63.                   keystring   =   Mid$(t,   lastpointer,   nullpointer   -   lastpointer)  
  64.                   '  
  65.                   '   Now   add   to   array  
  66.                   ArrayCount   =   ArrayCount   +   1  
  67.                   ReDim   Preserve   KeyArray(ArrayCount)  
  68.                   KeyArray(ArrayCount)   =   keystring  
  69.                   '  
  70.                   '   Find   next   null  
  71.                   lastpointer   =   nullpointer   +   1  
  72.                   nullpointer   =   InStr(nullpointer   +   1,   t,   Chr$(0))  
  73.               Loop  
  74.       End   If  
  75.       '  
  76.       '   Return   the   number   of   array   elements  
  77.       GetSection   =   ArrayCount  
  78.        
  79.   End   Function






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值