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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1208793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:57:58  来源:igfitidea点击:

Reading dll.config (not app.config!) from a plugin module

c#.net-2.0settingsappsettings

提问by Rodney Schuler

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.

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

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:

计划是在部署 .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();

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

那是行不通的。我正在努力解决整个 .NET 设置混乱。我想要一个食谱来完成这个任务。我还想要一个清晰解释(带有示例)的链接,说明设置应该如何在 .NET 2.0 中工作

采纳答案by Richard

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.

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

To open a named config file:

要打开命名的配置文件:

  • Reference System.Configuration.dllassembly.
  • 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);
    }
    
  • 参考System.Configuration.dll装配。
  • 使用 System.Configuration
  • 创建如下代码:

    Configuration GetDllConfiguration(Assembly targetAsm) {
      var configFile = targetAsm.Location + ".config";
      var map = new ExeConfigurationFileMap {
        ExeConfigFilename = configFile
      };
      return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
    }
    

回答by Mohamad Chami

1- open app.config file in visual studio

1-在visual studio中打开app.config文件

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

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

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

3- in your code c#

3- 在你的代码 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"

并且不要忘记为“ConfigurationManager”和“Assembly”添加这两个用途

  • using System.Configuration;
  • using System.Reflection;
  • 使用 System.Configuration;
  • 使用 System.Reflection;

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

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

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

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

  • open the file "MyProj.dll.config"
  • then update your configurations
  • 打开文件“ MyProj.dll.config
  • 然后更新您的配置