Wall
Wall

Reputation: 313

C# App.config Embedded Resource in Visual Studio

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?

Upvotes: 1

Views: 2427

Answers (2)

Bijan Ghasemi
Bijan Ghasemi

Reputation: 46

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");

Upvotes: 0

santosh kumar patro
santosh kumar patro

Reputation: 8241

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

Upvotes: 1

Related Questions