【问题标题】:How to read the config value of another project from the same solution?如何从同一个解决方案中读取另一个项目的配置值?
【发布时间】:2017-07-19 01:20:45
【问题描述】:
var userName = ConfigurationManager.AppSettings["FileUploadRoot"];

我正在为变量用户名获取空值。关键 FileUploadRoot 在另一个项目的 web 配置条目中,上面的代码在另一个项目的类文件中。那么你能告诉我如何从网络配置文件中获取值

【问题讨论】:

  • 这段代码将从项目中读取AppSettings,而不是从另一个项目中写入。在您的项目中,“FileUploadRoot”键不存在。
  • 是的,我明白。所以你能告诉我如何从另一个项目的网络配置中读取它

标签: c# asp.net asp.net-mvc


【解决方案1】:

最好的方法是将配置值也包含在您的第一个项目中,使其独立。

您可以在具有 web.config 文件的项目中使用静态类,并在 internal 属性中公开 web.config 中的值

你可以这样使用:

  static class Utility
    {
        internal static string FileUploadRoot
        {
            get
            {
                return ConfigurationManager.AppSettings["FileUploadRoot"];
            }
        }
    }

另一种解决方案是将文件添加为项目的链接。

  1. 在解决方案资源管理器中右键单击您的项目。
  2. 选择添加现有项目...
  3. 在 OpenFileDialog 中选择 web.config 文件,但在对话框中选择 Add as Link 选项。

【讨论】:

    【解决方案2】:

    为此有内置功能:

    string otherExe = @"C:\projects\otherExe\bin\Debug\BillsAndStatementsArchiver.exe";
    Configuration otherConfig = ConfigurationManager.OpenExeConfiguration(otherExe);
    string fileUploadRoot = otherConfig.AppSettings.Settings["fileUploadRoot"].Value;
    

    【讨论】:

      猜你喜欢
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多