我的解决方案中有两个项目,为了举例,我将它们称为项目A和B。
项目B引用了项目A。 项目B可以访问项目A的app.config属性吗?
我希望能够访问项目A的app.config中的应用程序密钥字符串。
string tfsUri = ConfigurationManager.AppSettings["TfsUri"];
我的解决方案中有两个项目,为了举例,我将它们称为项目A和B。
项目B引用了项目A。 项目B可以访问项目A的app.config属性吗?
我希望能够访问项目A的app.config中的应用程序密钥字符串。
string tfsUri = ConfigurationManager.AppSettings["TfsUri"];
一般而言,这不是一个好主意,因为它会在项目之间引入硬性依赖。所以如果您可以复制和粘贴配置值,这将使您的项目更加自包含(但是这会导致配置值的重复)。
您还可以自动化此过程,以便在构建项目时自动解决配置依赖关系。
尽管如此,还有其他选项,在每种情况下您可能更喜欢使用其他方法。您的其他选项包括:
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);