天天看點

autoit基礎 IniRead 讀取ini配置檔案中的資料

  •        OS : Windows 10 Version 21H1
  •       AutoIt : v3.3.16.0
  •        blog : 師萬物
  •  typesetting : Markdown

script

  • my.ini
[section]
key1=value1
key2=value2
key3=value3
[section1]
key1=value1
key2=value2
key3=value3_new

           
#include<Array.au3>

Global $result

#comments-start
IniRead ( "filename", "section", "key", "default" )
Reads a value from a standard format .ini file.
#comments-end

;讀取指定ini檔案 - 指定section - 指定key的value值
$result = IniRead(@DesktopDir & "\my.ini","section", "key1", "the requested key is not found")
ConsoleWrite($result)

ConsoleWrite(@CRLF)

#comments-start
IniReadSection ( "filename", "section" )
Reads all key/value pairs from a section in a standard format .ini file.
#comments-end
;讀取ini檔案中指定Section中的key-value
;$sectionArr[0][0]是個數
Local $sectionArr[][] = IniReadSection(@DesktopDir & "\my.ini","section")

If Not @error Then                
	For $i = 1 To $sectionArr[0][0]
		ConsoleWrite($sectionArr[$i][0] & "-" & $sectionArr[$i][1])
		ConsoleWrite(@CRLF)
	Next
EndIf

ConsoleWrite(@CRLF)

#comments-start
IniReadSectionNames ( "filename" )
Reads all sections in a standard format .ini file.
#comments-end
;讀取ini檔案中所有的Section名
Local $sectionNamesArr[] = IniReadSectionNames(@DesktopDir & "\my.ini")
If Not @error Then		
	For $i = 1 To $sectionNamesArr[0]
		ConsoleWrite($sectionNamesArr[$i])
		ConsoleWrite(@CRLF)
	Next
EndIf


           

Go

>"D:\Develop\AutoIt3\SciTE\..\AutoIt3.exe" /ErrorStdOut "E:\autoit\demo.au3"    
value1
key1-value1
key2-value2
key3-value3

section
section1
>Exit code: 0

           

學習資源

  • autoit
  • autoit-docs
  • IT天空
  • msdn
  • 51CTO社群