Function IniWrite

Function Reference


IniWrite

Writes a value to a standard format .ini file.

IniWrite ( "filename", "section", "key", "value" )

Parameters

filename The filename of the .ini file.
section The section name in the .ini file.
key The key name in the .ini file.
value The value to write/change.

Return Value

Success: 1.
Failure: 0 if file is read-only.

Remarks

A standard ini file looks like:
[SectionName]
Key=Value


If the file does not already exist, it is created so long as the directory exists. Keys and/or sections are added to the end and are not sorted in any way.

If you would like to wrap quotes around an ini value, then you need to double up the quotation marks as the first set of quotation marks are stripped. For example: ""This is a test"" will produce "This is a test" when using IniRead() or such-like.

Leading and trailing whitespace is stripped. In order to preserve the whitespace, the string must be quoted. For example, "    this is a test" will preserve the whitespace but per above, the quotation marks are stripped.

Multi-line values are not possible.

If you want to use an ini file with Unicode encoding, first create an .ini file by using the FileOpen() function with the mode parameter set to a Unicode parameter.

Related

IniDelete, IniRead, IniReadSection, IniReadSectionNames, IniRenameSection, IniWriteSection

Example

#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

Example()

Func Example()
    ; Create a constant variable in Local scope of the filepath that will be read/written to.
    Local Const $sFilePath = _WinAPI_GetTempFileName(@TempDir)

    ; Write the value of 'AutoIt' to the key 'Title' and in the section labelled 'General'.
    IniWrite($sFilePath, "General", "Title", "AutoIt")

    ; Read the INI file for the value of 'Title' in the section labelled 'General'.
    Local $sRead = IniRead($sFilePath, "General", "Title", "Default Value")

    ; Display the value returned by IniRead.
    MsgBox($MB_SYSTEMMODAL, "", "The value of 'Title' in the section labelled 'General' is: " & $sRead)

    ; Delete the key labelled 'Title'.
    IniDelete($sFilePath, "General", "Title")

    ; Read the INI file for the value of 'Title' in the section labelled 'General'.
    $sRead = IniRead($sFilePath, "General", "Title", "Default Value")

    ; Display the value returned by IniRead. Since there is no key stored the value will be the 'Default Value' passed to IniRead.
    MsgBox($MB_SYSTEMMODAL, "", "The value of 'Title' in the section labelled 'General' is: " & $sRead)

    ; Delete the INI file.
    FileDelete($sFilePath)
EndFunc   ;==>Example