从c#交互式窗口访问配置文件

时间:2015-12-23 17:42:29

标签: c# visual-studio-2015 roslyn c#-interactive

我试图在VS2015 c#交互式窗口中运行一些代码,该窗口使用存储在我使用ConfigurationManager函数引用的dll的app.config文件中的连接字符串。

如果我查看public class TestVm { public string LT { get; set; } public int PID { get; set; } public List<KeyValuePair<string,string>> VP { get; set; } public int SR { get; set; } public int ER { get; set; } public string SB { get; set; } } [HttpPost] public IHttpActionResult GetDocs(TestVm VM) { //your code here } $.ajax({ //other code removed for readability contentType: 'application/json', type: 'POST', data: JSON.stringify( postdata), }); var postdata = { "LT": "c40210542a802cac4ca5fc96eaa2c3bfef592418b5", "PID": 1158341, // use 'Key' and 'Value' for correct bind with List<KeyValuePair> "VP":[{"Key":"RID","Value":"1158341"}], "ER": 100, "SR": 1, "SB": "DOCNAME ASC", } 中有一个连接字符串,但它是另一个必须从另一个配置文件引用的连接字符串。

1 个答案:

答案 0 :(得分:0)

如果您想要显式加载特定程序集的配置并明确访问它,则可以使用OpenExeConfiguration methodConfigurationManager class

使用此test.dll.config配置文件获取test.dll程序集:

<configuration>
  <connectionStrings>
    <add
       name="MyConnectionString"
       connectionString="my connection string"
       providerName="System.Data.SqlClient"
   />
  </connectionStrings>
</configuration>

您可以像这样加载配置文件:

> #r "c:\temp\test.dll"
> #r "System.Configuration"
> using System.Configuration;
> ConfigurationManager.OpenExeConfiguration(@"c:\temp\test.dll").ConnectionStrings.ConnectionStrings["MyConnectionString"].ConnectionString
"my connection string"