先将非标准ini转换成内置函数可以处理的格式。
处理完毕后再转换回去。
可以这样:#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
Global $FilePath = @ScriptDir&"\setup.ini"
If FileExists($FilePath) Then FileDelete($FilePath)
;写入实例
IniWrite($FilePath, "www", "www.ljbaidu.com", "")
IniWrite($FilePath, "www", "www.autoitx.com", "")
IniWrite($FilePath, "ip", "127.0.0.1", "")
IniWrite($FilePath, "ip", "192.168.1.1", "")
_ini_to_file($FilePath)
;显示非标准ini实例
ShellExecute($FilePath)
Sleep(2000)
_file_to_ini($FilePath);转换到标准
IniDelete($FilePath, "www", "www.ljbaidu.com")
IniDelete($FilePath, "ip", "192.168.1.1")
IniWrite($FilePath, "ip", "192.168.100.1", "")
IniWrite($FilePath, "www", "www.163.com", "")
IniWrite($FilePath, "www", "www.qq.com", "")
_ini_to_file($FilePath);转换到非标准
ShellExecute($FilePath)
Func _file_to_ini($sFilePath)
If Not FileExists($sFilePath) Then Return False
Local $aArray = FileReadToArray($sFilePath)
Local $iLineCount = @extended
If @error Then
MsgBox($MB_SYSTEMMODAL, "", "读取文件时出现错误. @error: " & @error) ; 读取当前脚本文件时发生错误.
Else
For $i = 0 To $iLineCount - 1 ; 遍历数组. 也可以使用 UBound($aArray).
$temp = StringStripWS ( $aArray[$i], 3 )
If $temp <> "" Then
If StringLeft ( $temp, 1 ) == "[" and StringRight ( $temp, 1 ) == "]" Then
Else
$aArray[$i] &= "="
EndIf
EndIf
Next
If FileExists($sFilePath) Then FileDelete($sFilePath)
For $i = 0 To $iLineCount - 1
FileWriteLine($sFilePath, $aArray[$i])
Next
EndIf
EndFunc ;==>Example
Func _ini_to_file($sFilePath)
If Not FileExists($sFilePath) Then Return False
Local $aArray = FileReadToArray($sFilePath)
Local $iLineCount = @extended
If @error Then
MsgBox($MB_SYSTEMMODAL, "", "读取文件时出现错误. @error: " & @error) ; 读取当前脚本文件时发生错误.
Else
For $i = 0 To $iLineCount - 1 ; 遍历数组. 也可以使用 UBound($aArray).
$temp = StringStripWS ( $aArray[$i], 3 )
If $temp <> "" Then
If StringLeft ( $temp, 1 ) == "[" and StringRight ( $temp, 1 ) == "]" Then
Else
If StringRight ( $aArray[$i], 1 ) == "=" Then $aArray[$i] = StringTrimRight ( $aArray[$i], 1 )
EndIf
EndIf
Next
If FileExists($sFilePath) Then FileDelete($sFilePath)
For $i = 0 To $iLineCount - 1
FileWriteLine($sFilePath, $aArray[$i])
Next
EndIf
EndFunc ;==>Example
|