在另一个解决方案中使用时无法从 DLL 读取配置

发布于 2024-12-08 20:46:14 字数 101 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

通知家属抬走 2024-12-15 20:46:14

.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's app.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....

南…巷孤猫 2024-12-15 20:46:14

如果您正在讨论在另一个 VS 项目或解决方案中使用 .NET 程序集及其配置,则可以将配置放入新项目的配置文件中。

如果您使用自动生成的强类型设置部分,则还需要复制整个设置部分及其 configSections 声明:

<configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <!-- VB-generated settings section -->
        <section name="HappyFunTime1.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        <!-- C#-generated settings section -->
        <section name="HappyFunTime2.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    </sectionGroup>
</configSections>

<applicationSettings>
    <HappyFunTime1.My.MySettings>
        <setting name="MySetting1" serializeAs="String">
            <value>Joy!</value>
        </setting>
    </HappyFunTime1.My.MySettings>
    <HappyFunTime2.Properties.Settings>
        <setting name="MySetting1" serializeAs="String">
            <value>Joy!</value>
        </setting>
    </HappyFunTime2.Properties.Settings>
</applicationSettings>

如果您使用 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:

<configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <!-- VB-generated settings section -->
        <section name="HappyFunTime1.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        <!-- C#-generated settings section -->
        <section name="HappyFunTime2.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    </sectionGroup>
</configSections>

<applicationSettings>
    <HappyFunTime1.My.MySettings>
        <setting name="MySetting1" serializeAs="String">
            <value>Joy!</value>
        </setting>
    </HappyFunTime1.My.MySettings>
    <HappyFunTime2.Properties.Settings>
        <setting name="MySetting1" serializeAs="String">
            <value>Joy!</value>
        </setting>
    </HappyFunTime2.Properties.Settings>
</applicationSettings>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文