Visual Studio 程序集强制安装目标框架

Visual Studio Assembly force-installs Target Framework

我将此程序集针对 .NET 3.5。该代码也适用于更高版本,但我喜欢它适用于 Windows XP。我的意思是,.NET 是向后兼容的,对吗?我可以在 Windows 8.1.

.NET 3.5 运行 应用程序

然而,当我运行自己的程序集时,它想要先安装.NET 3.5,即使我已经有4.5.1 已安装。

如果已经安装了较新的东西,我如何防止安装 3.5,同时保持 3.5 兼容?

目标 .NET 版本是应用默认依赖的唯一版本。 Visual Studio 不会自动添加更高版本和向后兼容的版本。

通过将其他 .NET 版本添加到配置文件来手动执行此操作:

  1. 在 Visual Studio 菜单栏上:
    • 选择项目;
    • 添加新项目;
    • 从左侧窗格中选择常规
    • 选择应用程序配置文件
    • 将配置文件命名为appName.exe.config.
  2. 像这样为每个 .NET 版本添加一个项目:

代码:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v2.0.50727"/>
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>

MSDN: Configure an App to Support .NET Framework 4 or 4.5


The .NET Framework 4.5 and its point releases are backward-compatible with apps that were built with earlier versions of the .NET Framework. In other words, apps and components built with previous versions will work without modification on the .NET Framework 4.5. However, by default, apps run on the version of the common language runtime for which they were developed, so you may have to provide a configuration file to enable your app to run on the .NET Framework 4.5. For more information, see the Version compatibility for apps section earlier in this article.

See MSDN