2

If I have a class library with an app.config file (I know it's not ideal, just bear with me for a moment) which has settings values created by using the projects Settings tab and accessed like this:

Public Shared Function GetMySetting(key As String) As String
    Dim value As String = My.Settings.Item(key)
    If value = String.Empty Then value = "Setting " & key & " not found."
    Return value
End Function

I then retrieve the settings from an application like this:

sb.AppendLine("GetMySetting: " & Library.Settings.GetMySetting("SettingC"))

The settings from the app.config file of the library project are definitely not copied into the app.config file of the application but I can still retrieve the My.Settings from the libraries app.config.

So I added a GetConfigFileName function to the library:

Public Shared Function GetConfigFileName() As String
    Return AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
End Function

and retrieved it in the application:

sb.AppendLine("Library Config File: " & Library.Settings.GetConfigFileName)

but that returns the application's config file.

How can I determine which .config file the library is reading from when it calls My.Settings...?

Or are they compiled into the DLL?

Carl Onager
  • 4,112
  • 2
  • 38
  • 66
  • A default setting is probably compiled into the DLL - it'll be inside the hidden designer file I suspect. The configuration settings should override this default setting. – SpaceBison Jun 24 '13 at 10:15
  • It's a trifle odd, I was expecting not to be able to retrieve settings from the library when using My.Settings in the way I was – Carl Onager Jun 24 '13 at 10:16
  • Are you using Visual Studio? If you globally find the setting key name (by name) - you'll probably find a code-based version - it will probably be called `Global.System.Configuration.DefaultSettingValueAttribute` – SpaceBison Jun 24 '13 at 10:26
  • There is such a property in the Settings.Designer.vb file but the class it's in (MySettings) inherits from ApplicationSettingsBase which must contain the file handling code (if there is any) – Carl Onager Jun 24 '13 at 12:17

2 Answers2

1

but that returns the application's config file. How can I determine which .config file the library is reading from when it calls My.Settings...?

  • By default, it will be always the main.exe.config.

    Take a look at this for detailed answer C# DLL config file for more informations

  • You can use external config files. This article on MSDN explains how.

EDIT

You're misunderstanding the situation, reread this line: The settings from the app.config file of the library project are definitely not copied into the app.config file of the application but I can sti retrieve the My.Settings from the libraries app.config.

You are correct. Your settings in the dll.config will not be copied automatically in the app.config.

Concerning the value that you are able to get from your dll, I am still looking for more official answer, but the answer seems to be:

the 'old' values (the ones you define at development time) are hard coded. If the franework isn't able to access or open the config file it will use the defaults instead. This will always happen if you use settings in a dll.

Take a look at this Settings.Default.<property> always returns default value instead of value in persistant storage (XML file)

Community
  • 1
  • 1
Chris
  • 8,527
  • 10
  • 34
  • 51
  • You're misunderstanding the situation, reread this line: The settings from the app.config file of the library project are definitely not copied into the app.config file of the application but I can still retrieve the My.Settings from the libraries app.config. – Carl Onager Jun 25 '13 at 07:35
  • Nicely found, this has reinforced my new rule of thumb: 'Don't use My.Settings'. I'll be sticking to ConfigurationManager.Appsettings from now on – Carl Onager Jun 26 '13 at 07:37
0

Edited after reading comments.

I think I see what your after. The My.Settings will used the compiled values from your class libs settings as default values. I believe these are compiled in. You can reflect the source though to see if it is setting a defaultvalue attribute on them.

You can override these by setting the values in the main exe's application file. If the settings aren't in the exes file, then you will use the default ones you specified in your class libs.

You shouldn't care about the file name. Just assume the settings are either defaulted or were overridden. If you need to know if they were overridden, that's another issue.

  • You're misunderstanding the situation, reread this line: The settings from the app.config file of the library project are definitely not copied into the app.config file of the application but I can still retrieve the My.Settings from the libraries app.config. – Carl Onager Jun 25 '13 at 07:36