C# interactive itself runs as a separate application with separate application configuration file. If you run this in C# interactive:
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
You will see something like:
"<path to VS>\\CommonExtensions\\Microsoft\\ManagedLanguages\\VBCSharp\\InteractiveComponents\\InteractiveHost.exe.Config"
So that's the config file being used. Of course it does not contain your variables, so code trying to do ConfigurationManager.AppSettings["foo"].ToString()
fails.
Usual way to set config file at runtime is:
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", fullPathToYourConfig);
However, this should be done before any access to configuration file. When first access is made - file is being cached and subsequent changes to path will have no effect. Unfortunately, before giving you access to executing commands, C# interactive already works with that file.
There are various hacks with reflection to reset that cache. For example (copied verbatim from here):
public static void ChangeConfigTo(string path)
{
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);
typeof(ConfigurationManager)
.GetField("s_initState", BindingFlags.NonPublic |
BindingFlags.Static)
.SetValue(null, 0);
typeof(ConfigurationManager)
.GetField("s_configSystem", BindingFlags.NonPublic |
BindingFlags.Static)
.SetValue(null, null);
typeof(ConfigurationManager)
.Assembly.GetTypes()
.Where(x => x.FullName ==
"System.Configuration.ClientConfigPaths")
.First()
.GetField("s_current", BindingFlags.NonPublic |
BindingFlags.Static)
.SetValue(null, null);
}
With all that in mind, if you put that function in Program
class in your sample on github and do this in C# interactive:
Program.ChangeConfigTo(Path.GetFullPath("app.config"));
Your code will work as you expect. You can put this hack in separate script (.csx
) file and load it with "#load" if necessary.