0

suppose i have a project named dummyCode.sln, now when i compile the project i would get a dll named dummyCode.dll.

How can i read this DLL using c# and get the information of my web.config

for example if i have a web.config like this

<configuration>
 <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="employeeDB" value="Data Source=servername;Initial Catalog=employee;Persist Security Info=True;User ID=userid;Password=password;"/>
  </appSettings>
</configuration>

then i want to read al the values found inside the appsettings attribute, how can i do it, is it possible to do?

Lijin Durairaj
  • 4,910
  • 15
  • 52
  • 85
  • 1
    what you mean by _read the Dll_ ? Load dll to app domain and then read metadata ? – rahulaga-msft Mar 02 '18 at 06:47
  • 4
    A DLL is a class library - class libraries don't have their own config file, they use the config file of the application that is referencing them. – Tim Mar 02 '18 at 07:04

2 Answers2

1

This Dll will be used in some Exe or a Website. You need to added these config entries to that Exe's or Website's config file.

app.Config (for Exe) and web.config (for Website)

Once done, you can read these using:

string theValue = ConfigurationManager.AppSettings["KeyName"];

(Namespace: System.Configuration)

Bonus, you can create methods to read different info:

Example: Int

public static int GetIntConfiguration(string keyName)
{
    string value = ConfigurationManager.AppSettings[keyName] ?? "0";
    int theValue = 0;
    if (int.TryParse(value, out theValue))
    {
       return theValue;
    }
    return -1;
}

So a -1, tell you that there is an invalid value in config.

Similarly, you can create methods for other types.

EDIT: based on your comment,
If you need to open a custom config:

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration("dllPath.dll");

string value = config.AppSettings.Settings["key"].Value;

If it's a web.config, you can change the first line to:

System.Configuration.Configuration configWeb = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("");
Sunil
  • 3,404
  • 10
  • 23
  • 31
  • ok, when i compile my solution file, i will get a dll, which would have all the files along woth web.config, right? i want to read the dll and get the web.config information. i dont want to know the ways to read web.config from the application. i want ways to read the dll and the information of the web.config file inside the dll – Lijin Durairaj Mar 02 '18 at 07:37
  • Edited the post for extra details. – Sunil Mar 02 '18 at 08:03
  • @LijinDurairaj DLLs don't have config files. See my answer. – ADyson Mar 02 '18 at 09:18
1

As mentioned in the comments already, compiled DLLs of class library projects don't have their own .config file. If you made an app.config or web.config within that project, it is not included as part of what gets compiled into the DLL. The DLL only contains .NET code.

It wouldn't make any logical sense to do so - the settings files are meant to be per application, not per project. Otherwise they could conflict with each other, and/or not be adjustable to suit the application where the library is included.

P.S. This has been dealt with before on SO, here and here (and possibly elsewhere). There are a couple of suggestions of how you could achieve your aim, but also plenty of similar discussion about why it's not a very good idea.

ADyson
  • 57,178
  • 14
  • 51
  • 63