Location of app.config file used by referenced library for My.Settings?

≡放荡痞女 提交于 2020-01-11 11:04:05

问题


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?


回答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)




回答2:


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.



来源:https://stackoverflow.com/questions/17272956/location-of-app-config-file-used-by-referenced-library-for-my-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!