0

I'm fully aware this question has been asked multiple time on here, but I have scoured the internet and have yet to find a solution.

I run the following .csx file with scriptcs (just to test and make sure the ConfigurationManager works):

#load "C:\Tickets\LoadConfig.csx"

using System;
using System.Configuration;
Console.WriteLine(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
Console.WriteLine(ConfigurationManager.AppSettings.AllKeys);

Here is the LoadConfig.csx, I found it on here SO post and multiple people said they had good results.

#r "System.Configuration"

using System;
using System.IO;
using System.Linq;

var paths = new[] { Path.Combine(Environment.CurrentDirectory, "web.config"), Path.Combine(Environment.CurrentDirectory, "app.config") };
var configPath = paths.FirstOrDefault(p => File.Exists(p));

if (configPath != null)
{
    AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", configPath);

    var t = typeof(System.Configuration.ConfigurationManager);
    var f = t.GetField("s_initState", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
    f.SetValue(null, 0);

    Console.Write(configPath); // Here to make sure it found the app.config file properly
}

Here's the app.config too:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>
    <appSettings>
        <add key="testKey" value="testValue" />
    </appSettings>
</configuration>

However, when I run the first code block it tells me that the current configuration file is app.config and that the AllKeys property is System.String[]. I've made sure that all the files are in the same folder and that the app.config is written correctly too. I'm just stuck right now and not sure if there are any other solutions or if I'm completely overlooking something. If anyone has any suggestions they'd be much appreciated, thanks.

adefilippo
  • 41
  • 2
  • 6

1 Answers1

0

This is because you are printing ConfigurationManager.AppSettings.AllKeys directly, which is not a string so it just prints the object type.

You need to iterate around the keys with something like

var keys = ConfigurationManager.AppSettings.AllKeys;
foreach (var key in keys)
{
    Console.WriteLine(key);
}
Console.ReadLine();

or

ConfigurationManager.AppSettings.AllKeys.ToList().ForEach(k => Console.WriteLine(k));

Output:

testKey

Bassie
  • 9,529
  • 8
  • 68
  • 159
  • That's super weird, because if I run: `Console.WriteLine("keys data type " + keys.GetType());` `Console.WriteLine("keys count " + keys.Count());` `Console.WriteLine(keys[0]);` It returns saying: `keys data type System.String[]` `keys count 0` `[IndexOutOfRangeException]` I don't see any reason for it to throw an IndexOutOfRange on index [0] unless it's empty? Plus I ran what you specified and I still got the same results as before. – adefilippo Nov 16 '17 at 02:50
  • `keys count 0` would indeed imply that it was empty. – Richard Szalay Nov 16 '17 at 02:57
  • And `Console.WriteLine(ConfigurationManager.AppSettings["testKey"]);` comes back null. – adefilippo Nov 16 '17 at 03:00
  • @adefilippo That is weird - did you try starting a fresh project and doing the same? Maybe there is some permissions issue stopping the account running the app from reading the config file? – Bassie Nov 16 '17 at 03:03
  • Yeah, I'll try that and report back what happens. Thanks! – adefilippo Nov 16 '17 at 03:04
  • Huh, I went and made a new app.config, copied the contents of the LoadConfig.csx into a new file, and put it all in a new folder. Went into the scriptcs REPL and `Console.WriteLine(ConfigurationManager.AppSettings[0]);` still throws an IndexOutOfRange exception. Thought I might mention, I'm not doing this in a VS project, I'm setting up a C# file to run (with scriptcs) every 3 minutes to poll an email account for IT help desk tickets and I want it to be fully independent from the site I'm making. – adefilippo Nov 16 '17 at 03:17
  • @adefilippo Check the permissions on the file/folder where all this is happening. Guessing it's Windows? – Bassie Nov 16 '17 at 03:33
  • @Bassie Permissions are all good, and I'm running scriptcs from cmd as Administrator and it still gives off the index error. I might just forgo the config file and just add what I would be referencing straight into the .csx. Thanks for all the help though! If you do find something out don't hesitate to let me know. – adefilippo Nov 16 '17 at 03:42
  • @adefilippo another option would be to just use a normal text file and read from it insteead of using `ConfigurationManager` – Bassie Nov 16 '17 at 03:51
  • Smart, I'm just going to use a plain xml file and store them there. Parsing through that seems much simpler than trying to set up this mess. – adefilippo Nov 16 '17 at 04:11
  • @Bassie, so it turns out that it works if I remove the scriptcs_packages folder and scriptcs_packages.config from the parent folder then it runs, but if I put them back in it doesn't pull from appsettings. I'm using the following packages: `AngleSharp, BouncyCastle, HtmlSanitizer, MailKit, MimeKit` if you have any ideas as to why they'd be restricting ConfigurationManager from running I'd love to hear it. – adefilippo Nov 16 '17 at 15:33