1

Visual Studio has the option to apply a Build Action for the App.config file as "Embedded Resource", which means including in the same final exe the content of the App.config. Fine.

The problem is: how to read the data inside the embedded App.config? For example an appSetting value from a given key?

The code I used before to read from the App.config (the one phisically written on the disk which usually is nameoftheprogram.exe.config), seems to be not working anymore.

string s = System.Configuration.ConfigurationManager.AppSettings["mykey"];

Probably it must be re-adapted with other C# classes designed for this job.

Any ideas?

Wall
  • 293
  • 3
  • 13
  • 1
    Maybe you should take a look at this: https://stackoverflow.com/questions/1846975/how-do-you-embed-app-config-in-c-sharp-projects – Carlos Oliveira Jul 12 '20 at 15:12

2 Answers2

1

You can have interface IConfigUtility with method :

IConfigUtility.cs:

public interface IConfigUtility
{
    string LogFilePath
    {
        get;
    }

    string GetAppSetting(string key);
}

ConfigUtility.cs

using System;
using System.Configuration;

public class ConfigUtility : IConfigUtility
{
    Configuration config = null;
    public string LogFilePath
    {
        get
        {
            return GetAppSetting(@"Code to read the log file path");
        }
    }

    public ConfigUtility()
    {
        var exeConfigPath = this.GetType().Assembly.Location;
        try
        {
            config = ConfigurationManager.OpenExeConfiguration(exeConfigPath);
        }
        catch (Exception)
        {
        //handle error here.. means DLL has no satellite configuration file.
        }
    }

    public virtual string GetAppSetting(string key)
    {
        if (config != null)
        {
            KeyValueConfigurationElement element = config.AppSettings.Settings[key];
            if (element != null)
            {
                string value = element.Value;
                if (!string.IsNullOrEmpty(value))
                    return value;
            }
        }

        return string.Empty;
    }
}

Now you can use the above ConfigUtility.cs and read your appsettings key from the App.config file

santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143
  • Thanks for the quick reply. This approach only works if I set the Build Action to "none". If I set it to "Embedded Resource" the values returned are null; it looks like it cannot read it. Should I re-adapt this code or it is something related to the configuration? – Wall Jul 12 '20 at 17:14
  • You can re-adapt this code and I am sure it will work for you too :) – santosh kumar patro Jul 12 '20 at 18:57
0

samplecode in .netCore.

Suppose we have a file named Sample.json in the root of the project, this name can be any other name.

Sample.json:

{
  "mykey": "mykeyvalue"
}

Follow the steps.

  1. Read the value from the EmbeddedResource.

     Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"{Assembly.GetExecutingAssembly().GetName().Name}.Sample.json");
    
  2. Add value to Configuration.

             if (stream != null)
             {
                 config.AddJsonStream(stream);
             }
    

all code in Program.cs.

   public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"{Assembly.GetExecutingAssembly().GetName().Name}.Sample.json");
            if (stream != null)
            {
                config.AddJsonStream(stream);
            }
        });
    
}
  1. use value.

IConfiguration conifg with dependency injection.

   var mykey= conifg.GetValue<String>("mykeyvalue");