简体   繁体   English

如何强制应用程序使用.NET 3.5或更高版本?

[英]How to force an application to use .NET 3.5 or above?

Our application is built with VS 2008, uses Linq and has Target Framework set to .NET Framework3.5. 我们的应用程序是使用VS 2008构建的,使用Linq并将Target Framework设置为.NET Framework3.5。

It works OK when only .NET 3.5 or 4 is installed on the machine. 当只在机器上安装.NET 3.5或4时,它可以正常工作。

However, on machines where both .NET 2 (or 3.0) and .NET 4 are installed, the application is loaded with .NET 2, and crashes when Linq is accessed, as it looks for the .NET 3.5 libraries. 但是,在安装了.NET 2(或3.0)和.NET 4的计算机上,应用程序加载了.NET 2,并在访问Linq时崩溃,因为它查找.NET 3.5库。

Using the tag in app.config doesn't seem to help, as it specifies the CLR version, which is 2 in case of .NET 3.5. 在app.config中使用标签似乎没有帮助,因为它指定了CLR版本,在.NET 3.5的情况下为2。

Note that our installation verifies that .NET 3.5 or upper is installed. 请注意,我们的安装会验证是否已安装.NET 3.5或更高版本。

Is there a way to tell the application to load: 有没有办法告诉应用程序加载:

  • the highest CLR it finds, or 它找到的最高CLR ,或
  • CLR 4 if it is installed, and CLR 2 if CLR 4 is not installed, or CLR 4(如果已安装)和CLR 2(如果未安装CLR 4)或
  • CLR 2 if .NET 3.5 is installed and CLR 4 if .NET 3.5 is not installed 如果安装了.NET 3.5,则为CLR 2;如果未安装.NET 3.5,则为CLR 4

(Note that similar question is left unanswered in the Community Content section of the Element documentation ) (请注意, 在Element文档的Community Content部分中仍未回答类似的问题)

Forming the question led me to the answer. 形成这个问题让我得到答案。 As mentioned in the Element documentation , 元素文档中所述

When multiple versions of the runtime are supported, the first element should specify the most preferred version of the runtime, and the last element should specify the least preferred version. 当支持多个版本的运行时时,第一个元素应指定最优选的运行时版本,最后一个元素应指定最不喜欢的版本。

So the way to achieve the second option ("CLR 4 if it is installed, and CLR 2 is CLR 4 is not installed") is to reverse the order of the elements in app.config: 因此,实现第二个选项(“如果安装了CLR 4,CLR 2未安装CLR 4”)的方法是颠倒app.config中元素的顺序:

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

This way, .NET 4 will be loaded if it is installed, and an earlier version will be loaded if not. 这样,如果安装了.NET 4,它将被加载,否则将加载早期版本。

Another useful link is this page on MSDN. 另一个有用的链接是MSDN上的这个页面 This shows all of the values required in app.config if you want to just target the client profile or if you require the full profile. 如果您只想定位客户端配置文件或需要完整配置文件,则会显示app.config中所需的所有值。

.NET Framework 3.0和3.5版使用CLR的2.0.50727版。

如果您有一个非托管EXE调用.NET DLL,您还需要创建一个foo.exe.config文件,其中包含上面的<startup>...块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM