Skip to content

Instantly share code, notes, and snippets.

@stknohg
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stknohg/acc4f0e00398cf4d3afc to your computer and use it in GitHub Desktop.
Save stknohg/acc4f0e00398cf4d3afc to your computer and use it in GitHub Desktop.
PowerShellでアプリケーション構成ファイルを読み書きするサンプル
# 要Add-Type
Add-Type -AssemblyName System.Configuration
# アプリケーション構成ファイルを読み込む
$Map = New-Object System.Configuration.ExeConfigurationFileMap
$Map.ExeConfigFilename = ".¥Sample.config"
$Config = [System.Configuration.ConfigurationManager]::OpenMappedExeConfiguration($Map, [System.Configuration.ConfigurationUserLevel]::None)
# 設定値の取得、更新
$Setting = $Config.AppSettings.Settings["MY_KEY"]
If ($Setting -eq $null) {
$Config.AppSettings.Settings.Add("MY_KEY", "New Value");
} else {
$Setting.Value = "New Value";
}
# 保存
$Config.Save();
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<!-- サンプルの設定 -->
<add key="MY_KEY" value="Current Value" />
</appSettings>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment