2012-11-16 33 views
5

Tôi đang cố gắng sử dụng một assembly .NET 4.0 trong PowerShell ISE, và cố gắng để thay đổi tập tin cấu hình được sử dụng thông qua:Sử dụng CurrentDomain.SetData ("APP_CONFIG_FILE") không hoạt động trong PowerShell ISE

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

[Configuration.ConfigurationManager] :: ConnectionStrings.Count luôn trả về "1",
và "[Configuration.ConfigurationManager] :: ConnectionStrings [0] .Name" luôn trả về "LocalSqlServer" và tên ConnectionString đó không có trong ".tập tin cấu hình.

Lưu ý rằng việc thực thi tập lệnh PowerShell từ dấu nhắc lệnh PowerShell có chức năng như mong đợi. Đó là khi tôi thực thi nó từ bên trong PowerShell ISE, nó không hoạt động như mong đợi.

Trả lời

19

Đó là vì đường dẫn đến app.config cho PowerShell ISE đã được nạp và lưu trữ để thay đổi đường dẫn App.config sau đó sẽ không tạo sự khác biệt: stackoverflow.com/q/6150644/222748

Dưới đây là một kịch bản ví dụ mà sẽ xóa đường dẫn được lưu trong bộ nhớ cache để nó sẽ hoạt động trong 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

Điều này không có tác dụng đối với tôi – Trent

+0

Điều này đã hiệu quả đối với tôi. Tôi cũng gặp vấn đề tương tự khi sử dụng shell thông thường cũng như ISE. – rikkit

1

Tắt [0] hoạt động cho tôi.

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

Các vấn đề liên quan