引用库用于My.Settings的app.config文件的位置? [英] Location of app.config file used by referenced library for My.Settings?

查看:106
本文介绍了引用库用于My.Settings的app.config文件的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个包含app.config文件的类库(我知道这不理想,请耐心等待一下),该类库具有使用项目设置"选项卡创建的设置值,并且可以通过以下方式进行访问:

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"))

绝对不会将库项目的app.config文件中的设置复制到应用程序的app.config文件中,但我仍然可以从库app.config中检索My.Settings.

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.

所以我在库中添加了GetConfigFileName函数:

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)

但是返回应用程序的配置文件.

当库调用My.Settings ...时,如何确定该库正在读取哪个.config文件?

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

还是将它们编译到DLL中?

Or are they compiled into the DLL?

推荐答案

但是返回应用程序的配置文件.我如何确定 库在调用时从哪个.config文件读取 我的设置...?

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...?

  • 默认情况下,它将始终为main.exe.config.

    看看这个以获得详细答案 C#DLL配置文件 更多信息

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

    您可以使用外部配置文件.在MSDN上的本文 解释如何.

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

    编辑

    您误解了情况,请重读此行:设置 从库项目的app.config文件中绝对不是 复制到应用程序的app.config文件中,但我可以从库app.config中检索My.Settings.

    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.

    您是正确的.您在dll.config中的设置不会自动复制到app.config中.

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

    关于您能够从dll中获得的价值,我仍在寻找更正式的答案,但答案似乎是:

    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:

    旧"值(在开发时定义的值)是硬编码的.如果 franework无法访问或打开配置文件,它将使用默认设置 反而.如果您在dll中使用设置,这将总是发生.

    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.

    看看这个设置.默认.总是返回默认值,而不是持久性存储(XML文件)中的值

    这篇关于引用库用于My.Settings的app.config文件的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆