在库中使用 .Net Standard 1.4 并在应用程序中使用 .Net framework 4.6.1 时,无法加载文件 System.IO.FileSystem,版本 = 4.0.1.0

When using .Net Standard 1.4 in a library and .Net framework 4.6.1 in and application, unable to load file System.IO.FileSystem, Version=4.0.1.0

我有一个包含库和 2 个应用程序的解决方案。这些应用程序代表相同的程序,其中一个是通过 UAP10 构建的针对 Windows App Store,另一个是针对使用 .Net Framework 4.6.1 的 Microsoft Windows PC。我正在使用 Visual Studio 2017.

UWP 版本编译运行良好。

最初,.Net Framework 4.6.1 编译并运行。但是,当我从 System.IO 调用 File.Open 时,我收到以下错误消息:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.IO.FileSystem, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

带有 File.Run 的代码块在库代码 (.Net Standard 1.4) 中。我不确定是什么导致了这个问题。我认为我可以在引用 .Net Framework 4.6.1 的应用程序中使用 .Net Standard 1.4 库。


作为解决方法,我尝试在 .Net Framework 应用程序中安装 System.IO 的 Nuget 包。 System.IO 版本 4.0.1.0 甚至不是一个选项。有一个4.0.10的版本,但是安装了这个Nuget包就不行了。

那么,如何在引用 .Net Standard 1.4 库中的 System.IO 代码的 .Net Framework 4.6.1 应用程序中正确引用 System.IO 操作?

这通常发生在 "classic" csproj 项目中,当引用的项目需要覆盖覆盖系统定义的类型但不存在正确设置它的绑定重定向时。将所有包安装到 .net 框架应用程序时(例如 NETStandard.LibraryNETStandard.Library.NETFramework(对于即将推出的 .NET Standard 2.0)),这应该自动完成。

要强制生成绑定重定向,请将其添加到 .NET Framework 项目的 csproj 文件中(如果它是一个可执行项目,第二个 属性 应该无关紧要,但不会造成伤害):

<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>