Visual basic 6读写ini文件
ini文件是windows的系統(tǒng)配置文件, 被用來對操作系統(tǒng)或特定程序初始化或進(jìn)行參數(shù)設(shè)置. 在Windows系統(tǒng)中,INI文件是很多,最重要的就是“System.ini”、“System32.ini”和“Win.ini”。該文件主要存放用戶所做的選擇以及系統(tǒng)的各種參數(shù)。用戶可以通過修改INI文件,來改變應(yīng)用程序和系統(tǒng)的很多配置。
?
中間的數(shù)據(jù)格式一般為:
;注釋(Comments)
[Section1 Name]
KeyName1=value1
KeyName2=value2
...
[Section2 Name]
KeyName1=value1
KeyName2=value2
ini 文件可以分為幾個(gè) Section,每個(gè) Section 的名稱用 [] 括起來,在一個(gè) Section 中,可以有很多的 Key,每一個(gè) Key 可以有一個(gè)值并占用一行,格式是 Key=value,注釋以分號";"開頭。
Windows提供了幾個(gè)有用的API來讀寫操作INI文件:
GetPrivateProfileSection - 從 ini 文件中讀出整個(gè) Section 的內(nèi)容
WritePrivateProfileSection - 將一個(gè)整個(gè) Section 的內(nèi)容入 ini 文件的指定 Section 中
WritePrivateProfileString - 將一個(gè) Key 值寫入 ini 文件的指定 Section 中
?
'declarations?for?working?with?Ini?files
Private?Declare?Function?GetPrivateProfileSection()Function?GetPrivateProfileSection?Lib?"kernel32"?Alias?_
"GetPrivateProfileSectionA"?(ByVal?lpAppName?As?String,?ByVal?lpReturnedString?As?String,?_
ByVal?nSize?As?Long,?ByVal?lpFileName?As?String)?As?Long
Private?Declare?Function?GetPrivateProfileString()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?WritePrivateProfileSection()Function?WritePrivateProfileSection?Lib?"kernel32"?Alias?_
"WritePrivateProfileSectionA"?(ByVal?lpAppName?As?String,?ByVal?lpString?As?String,?_
ByVal?lpFileName?As?String)?As?Long
Private?Declare?Function?WritePrivateProfileString()Function?WritePrivateProfileString?Lib?"kernel32"?Alias?_
"WritePrivateProfileStringA"?(ByVal?lpApplicationName?As?String,?ByVal?lpKeyName?As?Any,?_
ByVal?lpString?As?Any,?ByVal?lpFileName?As?String)?As?Long
'reads?an?Ini?string
Public?Function?ReadIni()Function?ReadIni(Filename?As?String,?Section?As?String,?Key?As?String)?As?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
Function?ReadIniSection()Function?ReadIniSection(Filename?As?String,?Section?As?String)?As?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
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
Public?Sub?WriteIniSection()Sub?WriteIniSection(Filename?As?String,?Section?As?String,?Value?As?String)
????WritePrivateProfileSection?Section,?Value,?Filename
End?Sub
?
轉(zhuǎn)載于:https://www.cnblogs.com/iswszheng/archive/2009/04/24/1442737.html
總結(jié)
以上是生活随笔為你收集整理的Visual basic 6读写ini文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CCNP精粹系列之二十四--BGP的水平
- 下一篇: 大批量文件处理的7条建议