从插件模块读取 dll.config (不是 app.config!)

发布于 2024-07-29 22:26:29 字数 987 浏览 3 评论 0原文

我正在编写一个 C# .NET 2.0 .dll,它是大型应用程序的插件。 我的模块的 Visual Studio 项目有一个 app.config 文件,该文件与 MyProj.dll 一起复制到 MyProj.dll.config 中。

计划是在部署 .dll 后编辑 MyProj.dll.config。 我正在尝试从修改后的本地文件中读取我的设置。 我尝试拉出 LocalFilesSettingsObject 并将其应用程序名称更改为我的 .dll,如下所示:

        Properties.Settings config = Properties.Settings.Default;
        SettingsContext context = config.Context;
        SettingsPropertyCollection properties = config.Properties;
        SettingsProviderCollection providers = config.Providers;
        SettingsProvider configFile = Properties.Settings.Default.Providers["LocalFileSettingsProvider"];
        configFile.ApplicationName = Assembly.GetExecutingAssembly().GetName().Name;
        config.Initialize(context, properties, providers);
        config.Reload();

这不起作用。 我正在努力解决整个 .NET 设置混乱的问题。 我想要一个食谱来完成这项任务。 我还想要一个关于设置如何在 .NET 2.0 中工作的清晰解释(带有示例)的链接

I am writing a C# .NET 2.0 .dll that is a plug in to a Larger application. The visual studio project for my module has a app.config file which is copied to a MyProj.dll.config along side of MyProj.dll.

The plan is that MyProj.dll.config will be edited after the .dll is deployed. I am trying to read my settings from that modified local file. I have tried pulling out the LocalFilesSettingsObject and changing it's application name to my .dll like this:

        Properties.Settings config = Properties.Settings.Default;
        SettingsContext context = config.Context;
        SettingsPropertyCollection properties = config.Properties;
        SettingsProviderCollection providers = config.Providers;
        SettingsProvider configFile = Properties.Settings.Default.Providers["LocalFileSettingsProvider"];
        configFile.ApplicationName = Assembly.GetExecutingAssembly().GetName().Name;
        config.Initialize(context, properties, providers);
        config.Reload();

That is not working. I am struggling to wrap my head around the whole .NET Settings mess. I'd like a recipe to finish this task. I would also like a link to a clear explanation (with examples) of how settings are supposed to work in .NET 2.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

浅听莫相离 2024-08-05 22:26:29

您需要自己加载x.dll.config(使用配置API)。 所有自动文件处理(包括 .Settings)都是关于 machine.config/y.exe.config/user-settings 的。

要打开命名的配置文件:

  • 参考System.Configuration.dll程序集。
  • 使用System.Configuration
  • 创建如下代码:

    配置 GetDllConfiguration(Assembly targetAsm) { 
        var configFile = targetAsm.Location + ".config"; 
        var map = new ExeConfigurationFileMap { 
          ExeConfigFilename = 配置文件 
        }; 
        返回 ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); 
      } 
      

You will need to load the x.dll.config (with the Configuration API) yourself. All the automatic file handling (including the .Settings) is all about machine.config/y.exe.config/user-settings.

To open a named config file:

  • Reference System.Configuration.dll assembly.
  • Using System.Configuration
  • Create code like:

    Configuration GetDllConfiguration(Assembly targetAsm) {
      var configFile = targetAsm.Location + ".config";
      var map = new ExeConfigurationFileMap {
        ExeConfigFilename = configFile
      };
      return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
    }
    
舟遥客 2024-08-05 22:26:29

1- 在 Visual Studio 中打开 app.config 文件

2- 在“配置”标签中,在标签“appSettings”中添加您的配置,如下所示:

<configuration>
    <appSettings>
        <add key="UserName" value="aaa"/>
        <add key="Password" value="111"/>
    </appSettings>
</configuration>

3- 在您的编写 c# 代码

var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string userName = appConfig.AppSettings.Settings["UserName"].Value;
string password = appConfig.AppSettings.Settings["Password"].Value;

,并且不要忘记

  • 使用 System.Configuration 添加这 2 个“ConfigurationManager”和“Assembly”的用法;
  • 使用系统反射;

如果 System.Configuration 未显示,则必须在引用中添加引用“System.Configuration”

4-您可以按如下方式更新 dll 的配置:

  • 打开文件“MyProj” .dll.config
  • 然后更新您的配置

1- open app.config file in visual studio

2- in the "configuration" tag add your configurations in tag "appSettings" as bellow:

<configuration>
    <appSettings>
        <add key="UserName" value="aaa"/>
        <add key="Password" value="111"/>
    </appSettings>
</configuration>

3- in your code c#

var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string userName = appConfig.AppSettings.Settings["UserName"].Value;
string password = appConfig.AppSettings.Settings["Password"].Value;

and do not forgot to add this 2 usings for "ConfigurationManager" and for "Assembly"

  • using System.Configuration;
  • using System.Reflection;

if the System.Configuration does not show, you must add the reference "System.Configuration" in the References

4- you can update the configurations for the dll as bellow:

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