2012-11-16 37 views
5

動作しない私は、PowerShellのISEに.NET 4.0のアセンブリを使用しようとすると、を介して使用されている設定ファイルを変更しようとしています:CurrentDomain.SetData(「APP_CONFIG_FILE」)を使用すると、PowerShellのISEに

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $PathToConfig);  

[Configuration.ConfigurationManager] :: ConnectionStrings.Countは常に "1"、
を返し、 "[Configuration.ConfigurationManager] :: ConnectionStrings [0] .Name"は常に "LocalSqlServer"を返し、そのConnectionString名はmy ".config"ファイル。

PowerShellコマンドプロンプトからPowerShellスクリプトを実行すると、期待どおりに機能することに注意してください。 PowerShell ISEから実行すると、期待通りに動作しません。

答えて

19

のPowerShell ISE用のApp.configするためのパスをまだその後のapp.configパスを変更するとの違いをすることはありませんロードされ、キャッシュされているためです。ここで stackoverflow.com/q/6150644/222748

はクリアされますスクリプトの例ですキャッシュされたパスには、PowerShellのISEの下で動作するよう:

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $PathToConfig) 
Add-Type -AssemblyName System.Configuration 
[Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0) 
[Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null) 
([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"})[0].GetField("s_current", "NonPublic, Static").SetValue($null, $null) 
[Configuration.ConfigurationManager]::ConnectionStrings[0].Name 
+0

これは、これは私のために働い – Trent

+0

私のために動作しませんでした。私はまた、ISEと同様に通常のシェルを使用して同じ問題を抱えていました。 – rikkit

1

は私にとって[0]作品を離陸。

([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"}).GetField("s_current", "NonPublic, Static").SetValue($null, $null)

関連する問題