访问另一个项目的app.config属性?

26

我的解决方案中有两个项目,为了举例,我将它们称为项目A和B。

项目B引用了项目A。 项目B可以访问项目A的app.config属性吗?

我希望能够访问项目A的app.config中的应用程序密钥字符串。

string tfsUri = ConfigurationManager.AppSettings["TfsUri"];
2个回答

29

一般而言,这不是一个好主意,因为它会在项目之间引入硬性依赖。所以如果您可以复制和粘贴配置值,这将使您的项目更加自包含(但是这会导致配置值的重复)。

您还可以自动化此过程,以便在构建项目时自动解决配置依赖关系。

尽管如此,还有其他选项,在每种情况下您可能更喜欢使用其他方法。您的其他选项包括:


9
       var path = @"C:\Users\Stephen\source\repos\SensurityConfigurationTool\Configuration.UI\App.config";
        string directory = Path.GetDirectoryName(path);
        var pathRoot = Path.GetPathRoot(directory);
        string file = Path.GetFileName(path);


        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap
        {
            ExeConfigFilename = Path.Combine(Path.GetFullPath(directory + "\\" + file))
        };

        System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

你需要先获取相对路径,然后将其转换为绝对路径。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接