I am trying to access appSettings using the following syntax and I have used it before in my many website projects but not in a class library project. In this class library project I cannot even access the AppSettings Keys. Is there any way I could access the AppSettings Key from my class? Is the class library project’s app.config or project structure behave in a different way?
Dim SharedDrive As String = ConfigurationManager.AppSettings("scriptsfolder")
<configuration>
<appSettings>
<add key="scriptsfolder" value="C:\BadTempScripts"/>
</appSettings>
I have already added Project Reference to System.Configuration and imported in my class. When I run my code it says, “Object not set to an instance of any object”.
A library runs in the context of an application, not by itself.
You need to put the configuration in the
.configfile of the application that will use the library.So, if your library is
lib.dlland the application ismyApp.exe, you should use a.configfilemyApp.exe.config.As an alternative for using a
.configfile, consider passing in the configuration as a dependency to your library types.You have a specific bug in your code, in that you are trying to call a key
Scriptswhere your configuration has a keyscriptsfolder.