在另一个解决方案中使用时无法从 DLL 读取配置
我有一个 Dll,它被添加到另一个解决方案中,现在我用于读取配置文件的代码位于 DLL 中,
但我的配置文件位于当前解决方案中,
正确的解决方案应该是什么?
I have a Dll which is added into another solution now my code for reading the configuration file is in DLL
But my configuration file is in current solution
what should be the proper solution ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.NET 中的 DLL 不能有自己的配置 - 不会使用和解释
your.dll.config
文件。.NET 配置的基本前提是主机应用程序(您的主 EXE)应在其
app.exe.config
文件中包含其所有配置。所以你需要复制 &将 DLL 配置粘贴到主应用程序的app.config
文件中,应该没问题。请参阅 Chris Ammerman 的另一个 SO 问题,其中包含一个精彩而冗长的解释,说明为什么需要 DLL 配置并不像乍看起来那么微不足道......
DLL's in .NET cannot have their own configuration - that
your.dll.config
file will not be used and interpreted.The basic premise in .NET configuration is that the host application (your main EXE) should have all its configuration in its
app.exe.config
file. So you need to copy & paste your DLL config into the main app'sapp.config
file and you should be fine.See this other SO question with a great, lengthy explanation by Chris Ammerman of why having a DLL config isn't as trivial as it may seem at first....
如果您正在讨论在另一个 VS 项目或解决方案中使用 .NET 程序集及其配置,则可以将配置放入新项目的配置文件中。
如果您使用自动生成的强类型设置部分,则还需要复制整个设置部分及其 configSections 声明:
如果您使用 appSettings,则可以将它们复制到新项目的 appSettings 中部分。
话虽如此,我同意 marc_s 的观点;请务必阅读他引用的文章。
If you're talking about using a .NET assembly and its configurations in another VS project or solution, you can put the configurations in the config file of the new project.
If you're using an auto-generated strongly-typed settings section, you'll need to copy the entire settings section and the configSections declaration for it as well:
If you're using appSettings, you can copy them into the new project's appSettings section.
With that said, I agree with marc_s; please do read the article he references.