1

I am programmatically generating a config file.

Program.cs

string config = new ConfigBuilder().GenerateConfigFile(false);

ConfigBuilder.cs

public string GenerateConfigFile(bool isSettingEnabled)
{
    return "<configuration>...</configuration>";
}

Result

<configuration>
  <appSettings>
    <add key="IsSettingEnabled" value="false" />
  </appSettings>
</configuration>

Using something like the ConfigurationManager, I was hoping to read settings from the config variable without first having to write a file to disk. In other words do something like:

ConfigurationManager.OpenFromMemory(config);
var isSettingEnabled = ConfigurationManager.AppSettings["IsSettingEnabled"];

However it seems that the ConfigurationManager can only deal with files.

Is there an easy way to interpret the config without having to parse the XML myself?

H H
  • 263,252
  • 30
  • 330
  • 514
Aetherix
  • 2,150
  • 3
  • 24
  • 44
  • If you only plan to keep the generated configuration file in memory, then why not just maintain a simple Dictionary instead? Or, you would eventually want to write this configuration file to the disk down the line? – Jinish Nov 26 '16 at 00:20
  • There's more that's happening than what I've shown. I just tried to keep the example clear and focus on the problem which is: can I read a config which is a string iso a file? – Aetherix Nov 26 '16 at 00:24
  • Right. In any case this isnt possible. The ConfigurationManager class doesnt itself read the configurations. The file path that we get using different methods (OpenExeConfiguration, OpenMappedConfiguration etc.) is evetually used by the Configuration class to read the physical file from the filepath. So, you couldnt possibly have the configuration in the memory stream or such and read it using the ConfigurationManager. You would eventually have to save it to disk if you plan to use the ConfigurationManager class – Jinish Nov 26 '16 at 00:29
  • Possible duplicate of [Change default app.config at runtime](http://stackoverflow.com/questions/6150644/change-default-app-config-at-runtime) – Eris Nov 26 '16 at 00:38

1 Answers1

1

I don't believe you can do this, so what I've done is created an IDisposable wrapper class that stores the configuration data in a temporary file on disk, and cleans up when the wrapper class is disposed.

It looks like this:

sealed class ConfigurationGenerator : IDisposable
{
    private string _tempFileName;

    public ConfigurationGenerator(string configurationData)
    {
        if (String.IsNullOrEmpty(configurationData)) throw new ArgumentNullException("configurationData");
        _tempFileName = Path.GetTempFileName();
        File.WriteAllText(_tempFileName, configurationData);
    }

    public System.Configuration.Configuration Generate()
    {
        var filemap = new ExeConfigurationFileMap
        {
            ExeConfigFilename = _tempFileName
        };
        var config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
        return config;
    }

    public void Dispose()
    {
        if (_tempFileName != null)
        {
            string s = _tempFileName;
            _tempFileName = null;
            File.Delete(s);
        }
    }
}

and you use it like this:

using(var configGenerator = new ConfigurationGenerator(configData))
{
    Configuration configuration = configGenerator.Generate();

    ... use the configuration data here ...
}
Joe
  • 122,218
  • 32
  • 205
  • 338