Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing config file from c# interactive window

I'm trying to run some code in the VS2015 c# interactive window which uses a connection string stored in the app.config file of a dll which I'm referencing using the #r function.

If I look in the ConfigurationManager there is a connection string in it but it's a different one which it must be referencing from another config file.

like image 811
Jon Mitchell Avatar asked Dec 23 '15 17:12

Jon Mitchell


1 Answers

If what you want is to explicitly load the configuration for a specific assembly and explicitly access it, you can use the OpenExeConfiguration method of the ConfigurationManager class.

Take a test.dll assembly with this test.dll.config configuration file:

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

You cam load the configuration file like this:

> #r "c:\temp\test.dll"
> #r "System.Configuration"
> using System.Configuration;
> ConfigurationManager.OpenExeConfiguration(@"c:\temp\test.dll").ConnectionStrings.ConnectionStrings["MyConnectionString"].ConnectionString
"my connection string"
like image 161
Paulo Morgado Avatar answered Nov 15 '22 12:11

Paulo Morgado