我正在将应用程序从 .NET Framework 移植到 .NET Core(标准版)。
在应用程序中,我们有以下代码
public LogMessageListenerFromConfig()
: this(AppDomain.CurrentDomain.BaseDirectory.ConfigurationFile, LoggingSection.DefaultName)
{ }
public LogMessageListenerFromConfig(string section)
: this(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, section)
{ }
public LogMessageListenerFromConfig(string filename, string section)
{
// todo: set up a file watcher and refresh the listeners (etc.) when it changes.
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = filename };
var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
_section = LoggingSection.Section(configuration, section);
Refresh();
}
除了以下声明之外,这似乎与 .NET Core 兼容
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
AppDomain 上不再有 SetupInformation
。很公平,我读到它会导致问题等。但是,我还能如何在实用程序类中获取应用程序配置文件名?
请注意,此类将在控制台和 asp.net(核心)应用程序中使用。
感谢任何帮助
斯蒂芬
最佳答案
如果您将库移植到 .Net Standard,也会出现同样的问题。解决方案是这样的; 安装这个 nuget 包 system.configuration.configurationmanager 然后你可以使用:
var conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var configFilePath = conf.FilePath;
关于c# - 如何在 .NET Core 中获取机器配置文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51024110/