【问题标题】:Modify a .Net Applications .exe.config file Settings Value via Powershell通过 Powershell 修改 .Net Applications .exe.config 文件设置值
【发布时间】:2012-07-27 10:39:36
【问题描述】:

我有一个 .Net 控制台应用程序,它有一个 App.Config / MyApplicationConsole.exe.config 文件。这个包含通过 VS 的属性管理器设置的设置,基本上看起来像这样:

<?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>

现在我想要/需要做的是修改“SettingsKeyXYZ”设置的(“True”)值,最好是通过 powershell(我的同事设置)。有谁知道如何做到这一点?我发现的只是 Web.Configs 的示例,它们似乎与 VS 创建的有点不同。

【问题讨论】:

  • xml 文本无效。第 10 行标签的结束标签在哪里 (My.Applications.Namespace.Properties.Settings)

标签: powershell app-config


【解决方案1】:

首先,xml 文本无效。第 10 行标记 (My.Applications.Namespace.Properties.Settings) 的结束标记在哪里。我更改了第 10 行以匹配结束标记。

加载文件(作为 xml),您必须将 'My.Applications.Namespace.Properties.Settings' 标记放在引号中,否则 powershell 将尝试将点之间的每个值解析为标记),将值更新为 False然后保存文件。

[xml]$xml = Get-Content c:\App.Config
$xml.configuration.applicationSettings.'My.Applications.Namespace.Properties.Settings'.setting.value='False'
$xml.Save('c:\App.Config')

【讨论】:

  • Shay,感谢您调整我的问题文本。关于您的回答:我看不到您如何在此处专门访问“SettingsKeyXYZ”设置(因为可能还有更多)。我该怎么做?
  • 您想将“SettingsKeyXYZ”更改为其他内容吗?
  • 哦,不,我已经更新了上面的示例 - 我可能有几个 节点,它们由它们的 name='SomeKey' 属性标识,我只想更新 - 一个特定节点的节点,在本例中是具有 name='SettingsKeyXYZ' 属性的节点。
  • $xyz = $xml.configuration.applicationSettings.'My.Applications.Namespace.Properties.Settings'.setting | Where-Object {$_.Name -eq 'SettingsKeyXYZ'};现在您可以通过 $xyz 变量访问它的属性和属性
猜你喜欢
  • 1970-01-01
  • 2018-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
相关资源
最近更新 更多