1

I have a .Net console application which has an App.Config / MyApplicationConsole.exe.config file. This one contains settings set via the properties manager of VS, basically looking something like this:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="My.Applications.Namespace.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <My.Applications.Namespace.Properties.Settings>
      <setting name="SettingsKeyABC" serializeAs="String">
        <value>SomeOtherValue</value>
      </setting>
      <setting name="SettingsKeyXYZ" serializeAs="String">
        <value>True</value>
      </setting>
    </Siemens.Med.CTE.PMP.Applications.JobExecutor.Properties.Settings>
  </applicationSettings>
  <system.diagnostics>
    <trace>
      <listeners>
        <add name="Gibraltar" type="Gibraltar.Agent.LogListener, Gibraltar.Agent" />
      </listeners>
    </trace>
  </system.diagnostics>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
</configuration>

Now what I want/need to do is modify the ("True") value for the "SettingsKeyXYZ" setting, preferably via powershell (as my colleague set up). Does anyone know how to do this? All I found were sample for Web.Configs which seem a tad different than the ones created by VS.

1
  • The xml text is not valid. Where's the closing tag of line 10 tag (My.Applications.Namespace.Properties.Settings) Commented Jul 27, 2012 at 11:02

1 Answer 1

2

First, The xml text is not valid. Where's the closing tag of line 10 tag (My.Applications.Namespace.Properties.Settings). I changed line 10 to match the closing tag.

Load the file (as xml), you must put the 'My.Applications.Namespace.Properties.Settings' tag in quotes otherwise powershell will try to parse each value between the dots as a tag), update the value to False and then save the file.

[xml]$xml = Get-Content c:\App.Config
$xml.configuration.applicationSettings.'My.Applications.Namespace.Properties.Settings'.setting.value='False'
$xml.Save('c:\App.Config')
Sign up to request clarification or add additional context in comments.

4 Comments

Shay, thanks for adjusting my question's text. Regarding your answer: I don't see how you access the 'SettingsKeyXYZ' setting here specifically (as there might be more). How would I do that?
You want to change 'SettingsKeyXYZ' to something else?
Oh no, I've updated the example above - I might have several <settings ..> nodes which are identified by their name='SomeKey' attribute & I want to update only the <value>-Node of one particular one, in this example the one with the name='SettingsKeyXYZ' attribute.
$xyz = $xml.configuration.applicationSettings.'My.Applications.Namespace.Properties.Settings'.setting | Where-Object {$_.Name -eq 'SettingsKeyXYZ'}; Now you can access its properties and attributes via the $xyz variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.