VBA 讀寫ini檔案

2021-06-06 00:12:31 字數 992 閱讀 2554

在vba中必須通過呼叫win32 api來實現ini檔案讀寫。

option explicit

'read

private declare function getprivateprofilestring lib "kernel32" alias "getprivateprofilestringa" _

byval lpkeyname as string, _

byval lpdefault as string, _

byval lpreturnedstring as string, _

byval nsize as long, _

byval lpfilename as string) as long

'write

private declare function writeprivateprofilestring lib "kernel32" alias "writeprivateprofilestringa" _

byval lpkeyname as string, _

byval lpstring as string, _

byval lpfilename as string) as long

sub read()

dim rec as string

dim nc as long

rec = string(255, 0)

nc = getprivateprofilestring("boot", "shell", "", rec, 255, "c:\****\tmp1.ini")

msgbox rec

end sub

sub writeprofile()

dim nc as long

nc = writeprivateprofilestring("boot", "shell", "test1", "c:\******\tmp1.ini")

end sub

INI檔案讀寫

一 有必要了解ini檔案的結構 注釋 小節名 關鍵字 值 ini檔案允許有多個小節,每個小節又允許有多個關鍵字,後面是該關鍵字的值。值的型別有三種 字串 整型數值和布林值。其中字串存貯在ini檔案中時沒有引號,布林真值用1表示,布林假值用0表示。注釋以分號 開頭。二 定義 1 在inte ce的us...

讀寫ini檔案

using system using system.io using system.text using system.configuration using system.runtime.interopservices using system.collections.specialized us...

讀寫ini檔案

using system using system.io using system.text using system.configuration using system.runtime.interopservices using system.collections.specialized us...