具有以下 app.config:
<configuration>
<appSettings>
<add key="filepath" value="D:\Data" />
<add key="node2" value="..." />
...
</appSettings>
</configuration>
我想用新值替换具有 'filepath' 键的设置(在这种情况下通过替换 'D:\Data..')。新值应作为参数传递给 powershell 脚本。谢谢
具有以下 app.config:
<configuration>
<appSettings>
<add key="filepath" value="D:\Data" />
<add key="node2" value="..." />
...
</appSettings>
</configuration>
我想用新值替换具有 'filepath' 键的设置(在这种情况下通过替换 'D:\Data..')。新值应作为参数传递给 powershell 脚本。谢谢
根据评论更新:
$xml = [xml]'<configuration>
<appSettings>
<add key="filepath" value="D:\Data" />
<add key="filename" value="test.log" />
</appSettings>
</configuration>'
$xml.configuration.appSettings.add | foreach { if ($_.key -eq 'filepath') { $_.value = "C:\Data" } }
$xml.Save("C:\dell\NewApp.config")
==================================================== ==========================================
$xml = [xml]'<configuration>
<appSettings>
<add key="filepath" value="D:\Data" />
</appSettings>
</configuration>'
$xml.configuration.appSettings.add.value = "C:\Data"
$xml.Save("NewApp.config")
或者
$xml = [xml](Get-Content App.config)
$xml.configuration.appSettings.add.value = "C:\Data"
$xml.Save("NewApp.Config")