Jump to content

Error handling for IniWriteSection?


Recommended Posts

Hi,

I'm creating a tool to edit some ini files that belong to a program I'm working on...

Basically everything work fine except I can't find a way to check if the if the WriteIniSection operation was successful...

For Example, let's say the INI file is by mistake or some other reason Read Only, or the user don't have the right permission to write to the file or the folder it's in... Right now, in such a case The INI file, as expected, won't get updated, but I when I perform an if error or if not error checks I always get the msgbox for the if not error.

Here's the relevant code part:

 

IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "Conversion", $aConversion, 1)
    IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "AIO", $aAIO, 1)
    IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "Data", $aData, 1)
    IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "Redist", $aRedist, 1)
    IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "Split", $aSplit, 1)
    IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "Autorun", $aAutorun, 1)
    If Not @error Then
        MsgBox($MB_ICONINFORMATION, "Success", "Settings.ini Saved Successfully")
    Else
        If @error Then
            MsgBox($MB_ICONERROR, "Failure", "Settings.ini Could Not Be Saved!" & @CRLF & @CRLF & "Please Check If The File Is ReadOnly And That You Have Permission To Change It Or Its Location")
        EndIf
    EndIf

 

How can I perform a check to see if the ini file was written to successfully?

Thanks.

Ron Vollach
Microsoft Certified Professional (MCP)
Creator of Ultimate Conversion Compressor (UCC)
UCC Wikia

Link to comment
Share on other sites

Check if the return value equals 0

Local $hIniWrite
$hIniWrite = IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "Conversion", $aConversion, 1)
    If $hIniWrite = 0 Or @Error Then _IniWriteError("Conversion")
$hIniWrite = IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "AIO", $aAIO, 1)
    If $hIniWrite = 0 Or @Error Then _IniWriteError("AIO")
$hIniWrite = IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "Data", $aData, 1)
    If $hIniWrite = 0 Or @Error Then _IniWriteError("Data")
$hIniWrite = IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "Redist", $aRedist, 1)
    If $hIniWrite = 0 Or @Error Then _IniWriteError("Redist")
$hIniWrite = IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "Split", $aSplit, 1)
    If $hIniWrite = 0 Or @Error Then _IniWriteError("Split")
$hIniWrite = IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "Autorun", $aAutorun, 1)
    If $hIniWrite = 0 Or @Error Then _IniWriteError("Autorun")

Func _IniWriteError($sSection = "")
    MsgBox(4096, "Error Writing " & $sSection, "Error writing to Ini File or data format is invalid")
    Exit
EndFunc

 

Edited by Subz
Updated Script
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...