Jump to content

Read application.exe.config (XML)

Recommended Posts



I am struggling with reading a configuration file.
What i try to achieve is an array with two columns:

  • Column 0: Name of the value for ex. UserName
  • Column 1: the value <value></value> : MyUsername

The application.exe.config file

<?xml version="1.0" encoding="utf-8"?>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="IbUpload.Agent.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <supportedRuntime version="v4.0"/>
    <supportedRuntime version="v2.0.50727"/>
      <setting name="WebServerUrl" serializeAs="String">
      <setting name="DiarFileName" serializeAs="String">
        <value>%FacilityNo% - %CaptureDateTime%</value>
      <setting name="UserName" serializeAs="String">
      <setting name="Password" serializeAs="String">
      <setting name="ClientName" serializeAs="String">
      <setting name="UpdateUrl" serializeAs="String">

I have a working solution but i want to extend the flexibility of the function.
Now i have to know all the setting names,

; Read value
Local $sAppCfgUserName = _ApplicationConfig("Application.exe.config", "UserName")
Local $sAppCfgPassword = _ApplicationConfig("Application.exe.config", "Password")

; Write value
_ApplicationConfig("Application.exe.config", "UserName", "NewUsername")
_ApplicationConfig("Application.exe.config", "Password", "NewPassword")

#Region Read or Write to Application.Config.exe
Func _ApplicationConfig($sFile, $sXmlKeyName, $sXmlValue = Null)
    Local $aFile, $rReturn = False

    ; Check if configuration file exists, if not set @error to 1
    If Not FileExists($sFile) Then
        SetError(1) ; File does not exist
        Return False

    ; Read config
    If Not($sXmlValue) Then
        Local $oXML = ObjCreate("Microsoft.XMLDOM")
        If IsObj($oXML) Then
            $oParameters = $oXML.SelectNodes("//IbUpload.Settings/setting[@name='" & $sXmlKeyName & "']")

            For $oParameter In $oParameters
                $oValue = $oParameter.SelectSingleNode("./value")
                Return String($oValue.text) ; Return value
            SetError(2, @error) ; Error creating object
            Return ""
        ; Write Config
        $sXmlSearchValue = _ApplicationConfig($sFile, $sXmlKeyName)
        _FileReadToArray($sFile, $aFile)
        If @error Then
            SetError(3, @error)
            Return False

        For $a = 1 To $aFile[0]
            If StringInStr($aFile[$a], '<value>' & $sXmlSearchValue & '</value>') Then
                $sText = StringReplace(FileReadLine($sFile, $a), $sXmlSearchValue, $sXmlValue)
                _FileWriteToLine($sFile, $a, $sText, 1)
                If Not @error Then Return True
        Return $rReturn

EndFunc   ;==> _ApplicationConfig

In the end i want to compare two configuration files, now i have to know all "ParentNode" names and i am not able to check if there more Nodes in one of the two configuration files.

For example, one configuration file has the setting UpdateUrl2, i want able to detect that change.

      <setting name="UpdateUrl2" serializeAs="String">


Edited by Rijswijker

Share this post

Link to post
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


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.