Class Library Application Settings Don't Appear in app.exe.config  
Author Message
Virdee





PostPosted: Visual C# General, Class Library Application Settings Don't Appear in app.exe.config Top

I have a desire to define application settings within Class Library projects as well as the main application project. I was hoping the Class Library application settings would be accessible in the app.exe.config file. Is this possible

I created a winform application with a class library project. I added application settings using the settings designer in both projects. After compiling the application, I was able to access the application project's application settings in the app.exe.config. Where/how can I access the class library's application settings

I was able to work around this issue by manually transferring the section element and ClassLibraryNamespace.Properties.Settings elements from the class library app.config to the main app.config. I was able to verify the application was capable of reading the changes to app.exe.config realtime, but I would prefer to avoid having to maintain these settings manually.



Visual C#9  
 
 
missa





PostPosted: Visual C# General, Class Library Application Settings Don't Appear in app.exe.config Top

" I have a desire to define application settings within Class Library projects as well as the main application project."

You can add app.config in the dll project on a same way you add it to the exe project.

"Where/how can I access the class library's application settings "

You can use Configuration class that you can create on the following way.

public static Configuration CreateConfiguration( )

{

ExeConfigurationFileMap map = new ExeConfigurationFileMap();

//If this code is not in the same assembly where is your config file, just change FileInfo constructor parameter with the name of assembly which contains config file FileInfo configFile = new FileInfo(Assembly.GetExecutingAssembly().GetName().Name + ".dll.config");

map.ExeConfigFilename = configFile.FullName;

Configuration config=ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

if (config.HasFile) return config;

}

Then you create Configuration class instance using previous method and use that object to access dll configuration file just like you accessed application config file

If I understood your problem, maybe this can help you. Greetings!


 
 
James Curran





PostPosted: Visual C# General, Class Library Application Settings Don't Appear in app.exe.config Top

As I understand it, an application has only one application configuration file. Any attempt to read an app.config by an application (even if the code exists in library), will refer to that one config file. Remember, code library are used by applications, but only applications actually run.

This is perfectly natural. If there exists a setting that should be consistent through all uses of the library, regardless of the application that it's used it, then there's no point in putting it in a app.config file anyway --- you might as well just hard-code it.



 
 
Virdee





PostPosted: Visual C# General, Class Library Application Settings Don't Appear in app.exe.config Top

Thank you for your reply, but let me rephrase.

Where/how can I access the class library's application settings outside the application. I wish change these settings dynamically, not programmatically.

I have created a class library that processes credit cards. I would like to have the ability to alter the banking paramenters without having to recompile the application. It would seem natural to access the banking parameters within the app.exe.config file.

It is not convenient to initialize this credit card processor from the main application, since it does not directly access the credit card processor.


 
 
missa





PostPosted: Visual C# General, Class Library Application Settings Don't Appear in app.exe.config Top

"Where/how can I access the class library's application settings outside the application."

I would suggest you to create another simple application which accesses your config file just like any other XML file, but since you said:

"I wish change these settings dynamically, not programmatically.", and I suppose that you know that you can change values of the configuration file in notepad, or any other text editor, I really don`t know what did you mean.


 
 
Virdee





PostPosted: Visual C# General, Class Library Application Settings Don't Appear in app.exe.config Top

Thanks again for your response.

I believe the disconnect is that you are referring to entries within the <appSettings> section in the config file and I am referring to entries within the <applicationSettings> section in the config file. 

Entries in the <applicationSettings> are managed via the settings designer in VS2005, which can be accessed via the project's properties-->settings.  Once set, the settings are exposed in code as discrete variables such as

Int32 mySetting = Properties.Settings.Default.Mysetting;