Last active
August 29, 2015 14:08
-
-
Save stknohg/acc4f0e00398cf4d3afc to your computer and use it in GitHub Desktop.
PowerShellでアプリケーション構成ファイルを読み書きするサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 要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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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