2012-07-27 90 views
1

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

<?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”设置,最好通过powershell(如我的同事设置)。有谁知道如何做到这一点?我发现的所有内容都是Web.Configs的示例,它们与VS创建的不同。

+0

xml文本无效。第10行标记的结束标记(My.Applications.Namespace.Properties.Settings)在哪里? – 2012-07-27 11:02:31

回答

2

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

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

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

谢谢,谢谢你调整我的问题的文本。关于你的回答:我没有看到你如何访问'SettingsKeyXYZ'设置在这里具体(可能会更多)。我会怎么做? – 2012-07-27 12:40:30

+0

您想要将'SettingsKeyXYZ'更改为其他内容吗? – 2012-07-27 12:46:49

+0

哦,不,我已经更新了上面的例子 - 我可能有几个节点,它们的名称是='SomeKey'属性,我想只更新节点,在本例中, name ='SettingsKeyXYZ'属性。 – 2012-07-27 12:52:50

相关问题