如果未安装.NET,请不要启动应用程序

时间:2018-02-01 18:01:09

标签: c# .net windows

我有一个.NET Framework 4.6 WPF应用程序。问题是,如果我使用.NET Framework在Windows 7上启动应用程序< 4.6我收到一条非常通用的崩溃消息the application has stopped working

如果未安装所需的.NET版本,是否有办法阻止应用程序启动,而是显示更有意义的消息,说明the application requires a certain .NET version to run

我的app.config文件:

<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
  </startup>
</configuration>

似乎忽略了sku属性,Windows尝试使用用户计算机上存在的最新版.NET Framework启动应用程序。

应用程序部署为没有安装程序的单个文件应用程序,因此我无法创建一个可以检查.NET版本的单独的引导程序应用程序。

1 个答案:

答案 0 :(得分:0)

If you want your application's users to be able to use your application with .NET Framework 4.0+ installed on the target machine, you must target .NET Framework 4.0 in your application's build settings.

<PropertyGroup>
    ...
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    ...
</PropertyGroup>

It is not possible to run an application targeting .NET Framework 4.6 on a machine with only .NET Framework 4.0 installed (at least not if you use any of the APIs that have been added after .NET Framework 4.0).

Since all versions of .NET Framework above version 4.0 are in-place upgrades, the target system will always use the latest version, anyway. So, targeting .NET framework 4.0 will give you the desired behavior.

However, do note that some APIs are not available in .NET Framework 4.0 that are in 4.6, so you might need to make some changes or compromise and target .NET Framework 4.5. Keep in mind, .NET Framework 4, 4.5, and 4.5.1 are no longer officially supported by Microsoft. Targeting .NET Framework 4.5 will give you most of the APIs missing from 4.0, and will allow the user to use any version of .NET Framework 4.5+ on their system.

<PropertyGroup>
    ...
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    ...
</PropertyGroup>