我对 C# 中的 AppSettings 有疑问。 首先,我将描述我的情况。
我的解决方案包含一个可执行程序 program.exe
和一个 assembly.dll
.
程序引用程序集并使用它。程序集项目具有使用 Visual Studio 项目设置管理器设置的应用程序设置。现在,当我在 assembly\bin\release
中编译我的解决方案时文件夹中有一个 assembly.dll.config
包含我之前设置的设置的文件。
现在我不明白的是:在我引用 assembly.dll
的程序项目中我检查了CopyLocal=True
, 但在我的 program\bin\release
文件夹只有assembly.dll
但不是 assembly.dll.config 文件,但仍然是 assembly.dll
知道我在程序集项目应用程序设置中设置的设置。
现在我已经读过好几遍了,程序集总是访问可执行程序的设置,但是程序没有相应的设置,那么为什么程序集在没有assembly.dll.config
的情况下知道正确的设置呢?文件存在吗?
我假设设置在编译时编译到程序集中(当然),但是在我的 assembly\bin\release 文件夹中实际上有一个 assembly.dll.config 文件是没有意义的。
我尝试将此文件复制到我的 program\bin\release
中assembly.dll
所在的文件夹被复制到构建操作中,但是 assembly.dll
如果有 assembly.dll.config
就忽略文件存在于同一文件夹中。它始终使用编译时的设置。我只是不明白 assembly.dll.config
的用法文件。为什么在它从未对 assembly.dll´s
产生影响时创建它?行为?
最佳答案
默认值内置于 .dll 文件中。当然,您仍然可以更改这些设置,但您可以在 program.exe 配置中执行此操作,而不是通过在 configSections
/sectionGroup
中引用程序集设置。然后可以通过创建与该部分同名的 XML block ,在 applicationSettings
中更改程序集设置。
节组中的节标记可以简单地从程序集项目的 app.config 文件中复制。这样 token 、名称等将是正确的。 applicationSettings 部分也是如此。只需将其从程序集项目中的 app.config 复制到 program.exe 项目的 app.config 文件即可。
示例程序.exe.config:
<configuration>
<configSections>
... references to all dll settings ...
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="MyAssemblyNamespace.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
... more config stuff...
<applicationSettings>
... override your dll settings ...
<MyAssemblyNamespace.Properties.Settings>
<setting name="MaxUserNameLength" serializeAs="String">
<value>100</value>
</setting>
</MyAssemblyNamespace.Properties.Settings>
</applicationSettings>
关于c# - .NET-Assemblies 的默认 (assembly).dll.config 文件有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21627502/