Visual basic 6读写ini文件

ini文件是windows的系统配置文件, 被用来对操作系统或特定程序初始化或进行参数设置. 在Windows系统中,INI文件是很多,最重要的就是“System.ini”、“System32.ini”和“Win.ini”。该文件主要存放用户所做的选择以及系统的各种参数。用户可以通过修改INI文件,来改变应用程序和系统的很多配置。

 

中间的数据格式一般为:

;注释(Comments)

[Section1 Name]
KeyName1=value1
KeyName2=value2
...

[Section2 Name]
KeyName1=value1
KeyName2=value2

ini 文件可以分为几个 Section,每个 Section 的名称用 [] 括起来,在一个 Section 中,可以有很多的 Key,每一个 Key 可以有一个值并占用一行,格式是 Key=value,注释以分号";"开头。

Windows提供了几个有用的API来读写操作INI文件:

   GetPrivateProfileString - 从 ini 文件的某个 Section 取得一个 key 的字符串
   GetPrivateProfileSection - 从 ini 文件中读出整个 Section 的内容
   WritePrivateProfileSection - 将一个整个 Section 的内容入 ini 文件的指定 Section 中
   WritePrivateProfileString - 将一个 Key 值写入 ini 文件的指定 Section 中

 


' declarations for working with Ini files
ExpandedBlockStart.gifContractedBlock.gif
Private   Declare   Function GetPrivateProfileSection() Function GetPrivateProfileSection Lib "kernel32" Alias _
"GetPrivateProfileSectionA" (ByVal lpAppName As StringByVal lpReturnedString As String, _
ByVal nSize As LongByVal lpFileName As StringAs Long

ExpandedSubBlockStart.gifContractedSubBlock.gif
Private Declare Function GetPrivateProfileString()Function GetPrivateProfileString Lib "kernel32" Alias _
"GetPrivateProfileStringA" (ByVal lpApplicationName As StringByVal lpKeyName As Any, _
ByVal lpDefault As StringByVal lpReturnedString As StringByVal nSize As Long, _
ByVal lpFileName As StringAs Long

ExpandedSubBlockStart.gifContractedSubBlock.gif
Private Declare Function WritePrivateProfileSection()Function WritePrivateProfileSection Lib "kernel32" Alias _
"WritePrivateProfileSectionA" (ByVal lpAppName As StringByVal lpString As String, _
ByVal lpFileName As StringAs Long

ExpandedSubBlockStart.gifContractedSubBlock.gif
Private Declare Function WritePrivateProfileString()Function WritePrivateProfileString Lib "kernel32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As StringByVal lpKeyName As Any, _
ByVal lpString As Any, ByVal lpFileName As StringAs Long



'reads an Ini string
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public Function ReadIni()Function ReadIni(Filename As String, Section As String, Key As StringAs String
    
Dim RetVal As String * 255
    
    
Dim v As Long
    v 
= GetPrivateProfileString(Section, Key, "
NotFound ", RetVal, 255, Filename)
    
    ReadIni 
= Left(RetVal, v)
End Function

'reads an Ini sectionPublic
ExpandedSubBlockStart.gifContractedSubBlock.gif
Function ReadIniSection()Function ReadIniSection(Filename As String, Section As StringAs String
    
Dim RetVal As String * 255
    
    
Dim v As Long
    v 
= GetPrivateProfileSection(Section, RetVal, 255, Filename)
    
    ReadIniSection 
= Left(RetVal, v )
End Function


'writes an Ini string
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public Sub WriteIni()Sub WriteIni(Filename As String, Section As String, Key As String, Value As String)
    WritePrivateProfileString Section, Key, Value, Filename
End Sub


'writes an Ini section
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public Sub WriteIniSection()Sub WriteIniSection(Filename As String, Section As String, Value As String)
    WritePrivateProfileSection Section, Value, Filename
End Sub

 

转载于:https://www.cnblogs.com/iswszheng/archive/2009/04/24/1442737.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值