Dot-Net-Standard-2.0
netstandard 2.0 沒有 AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
我剛剛將一個
net451經典的 dotnet 項目升級為多目標項目,net461/netstandard2.0並取得了相當大的成功。但是,在通過以下
net461方式訪問舊配置文件時遇到了此編譯器錯誤:
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile這似乎不是問題
netstandard2.0:) 有人知道任何變通辦法或好的解決方案嗎?
根據這篇文章,我在尋找替代品時發現
PlatformServices.Default.Application.ApplicationBasePath,它應該是:
AppDomain.CurrentDomain.SetupInformation.ApplicationNameAssembly.GetEntryAssembly().GetName().Name就像你一樣,我剛剛發現第一個在最近的 2.0 中不存在,所以我將使用這個
GetEntryAssembly名稱....但是請記住,這
GetEntryAssembly可能是null在 xUnit 等測試執行程序中使用時,或者如果您的應用程式碼是從另一個非您的託管/啟動模組等執行的,則可能會出錯 - 所以這不是一個完美的替代品。
將 ConfigurationManager 添加為 NuGet
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'"> <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" /> </ItemGroup>在 C# 中使用 ConfigurationManager
var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); Log4NetLogger.Configure(configFile.FilePath);