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

查看:36
本文介绍了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 文件中,但我仍然可以从库 中检索 My.Settingsapp.config.

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天全站免登陆