今天我在我的机器上安装了.NET Framework 4.5,希望可以从Visual Studio 2010中使用它,因为它只是一个次要更新,不应该对Visual Studio 2010造成问题。不幸的是,即使手动删除某些4.0版本并添加相应的4.5版本组件,项目仍会引用原始的4.0版本组件。
是否可以从Visual Studio 2010中针对版本4.5进行目标设置?如果可以,该如何设置?我真的很想使用功能区...
今天我在我的机器上安装了.NET Framework 4.5,希望可以从Visual Studio 2010中使用它,因为它只是一个次要更新,不应该对Visual Studio 2010造成问题。不幸的是,即使手动删除某些4.0版本并添加相应的4.5版本组件,项目仍会引用原始的4.0版本组件。
是否可以从Visual Studio 2010中针对版本4.5进行目标设置?如果可以,该如何设置?我真的很想使用功能区...
每个版本的Visual Studio在Visual Studio 2010之前都与特定的.NET框架绑定。(VS2008是.NET 3.5,VS2005是.NET 2.0,VS2003是.NET1.1) Visual Studio 2010及以上版本允许针对先前的框架版本进行目标设置,但不能用于未来的发布。您必须使用Visual Studio 2012才能利用.NET 4.5。
我能想到的使用场景非常有限,但是假设你无法获得购买VS2012等工具的资金。如果是这种情况,并且你拥有Windows 7+和VS 2010,你可以尝试使用我编写的以下方法进行操作,该方法似乎可以正常工作(但我还没有完全使用这种方法部署应用程序)。
备份你的项目文件!!!
下载并安装Windows 8 SDK,其中包括.NET 4.5 SDK。
在VS2010中打开您的项目。
在您的项目中创建名为Compile_4_5_CSharp.targets
的文本文件,并将以下内容添加到其中。(或者只需从此处下载 - 确保从文件名中删除“.txt”扩展名):
<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Change the target framework to 4.5 if using the ".NET 4.5" configuration -->
<PropertyGroup Condition=" '$(Platform)' == '.NET 4.5' ">
<DefineConstants Condition="'$(DefineConstants)'==''">
TARGETTING_FX_4_5
</DefineConstants>
<DefineConstants Condition="'$(DefineConstants)'!='' and '$(DefineConstants)'!='TARGETTING_FX_4_5'">
$(DefineConstants);TARGETTING_FX_4_5
</DefineConstants>
<PlatformTarget Condition="'$(PlatformTarget)'!=''"/>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<!-- Import the standard C# targets -->
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- Add .NET 4.5 as an available platform -->
<PropertyGroup>
<AvailablePlatforms>$(AvailablePlatforms),.NET 4.5</AvailablePlatforms>
</PropertyGroup>
</Project>
卸载你的项目(右键->卸载)。
编辑项目文件(右键->编辑*.csproj)。
在项目文件中进行以下更改:
a. 将默认的Microsoft.CSharp.targets
替换为第4步创建的目标文件
<!-- Old Import Entry -->
<!-- <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> -->
<!-- New Import Entry -->
<Import Project="Compile_4_5_CSharp.targets" />
b. 将默认平台更改为.NET 4.5
<!-- Old default platform entry -->
<!-- <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> -->
<!-- New default platform entry -->
<Platform Condition=" '$(Platform)' == '' ">.NET 4.5</Platform>
在项目属性中指定其他框架时,添加AnyCPU
平台。此项应该在文件中的第一个<ItemGroup>
标记之前添加。
<PropertyGroup Condition="'$(Platform)' == 'AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
.
.
.
<ItemGroup>
.
.
.
保存更改并关闭*.csproj
文件。
重新加载项目(右键单击->重新加载项目)。
在配置管理器中(生成->配置管理器),确保为您的项目选择了“.NET 4.5”平台。
仍在配置管理器中,为“.NET 4.5”创建一个新的解决方案平台(可以基于“任意 CPU”),并确保选择了“ .NET 4.5 ”作为解决方案。
构建项目并检查是否有错误。
假设构建已完成,您可以通过向源代码添加对于4.5特定类的引用来验证您确实正在针对4.5进行目标定位:
using System;
using System.Text;
namespace testing
{
using net45check = System.Reflection.ReflectionContext;
}
当您使用“.NET 4.5”平台进行编译时,构建应该成功。当您在“Any CPU”平台下编译时,您应该会收到编译器错误: Error 6: The type or namespace name 'ReflectionContext' does not exist in
the namespace 'System.Reflection' (are you missing an assembly reference?)
<OutputPath>bin\</OutputPath>
到第一个PropertyGroup中,但是出现了以下错误:项目“项目名称”的OutputPath属性未设置。 - mparkuk提示:如果您想在VS2010中创建安装程序包,不幸的是它只支持.NET 4。为了解决这个问题,您需要将.NET 4.5添加为启动条件。
请在安装程序的启动条件中添加以下内容(右键单击查看,启动条件)。
在"搜索目标机器"中,右键单击并选择"添加注册表搜索"。
Property: REGISTRYVALUE1
RegKey: Software\Microsoft\NET Framework Setup\NDP\v4\Full
Root: vsdrrHKLM
Value: Release
Condition: REGISTRYVALUE1>="#378389"
InstallUrl: http://www.microsoft.com/en-gb/download/details.aspx?id=30653
Message: Setup requires .NET Framework 4.5 to be installed.
何时使用:
378389 = .NET Framework 4.5
378675 = 安装在Windows 8.1上的.NET Framework 4.5.1
378758 = 安装在Windows 8、Windows 7 SP1或Windows Vista SP2上的.NET Framework 4.5.1
379893 = .NET Framework 4.5.2
启动条件参考:http://msdn.microsoft.com/en-us/library/vstudio/xxyh2e6a(v=vs.100).aspx
我一直在苦恼VS2010/DNFW 4.5的整合问题,但终于解决了。从VS 2008开始,Visual Studio引入了一个用于缓存程序集的“引用程序集”文件缓存。这个文件缓存对于VS 2010而言位于\Reference Assemblies\Microsoft\Framework.NetFramework\v4.0。与从框架安装目录加载框架程序集不同,Visual Studio从此位置加载框架程序集。当微软表示VS 2010不支持DNFW 4.5时,他们实际上是指该目录没有在安装DNFW 4.5时进行更新。一旦您使用更新后的DNFW 4.5文件替换此位置中的文件,您将发现VS 2010能够轻松地使用DNFW 4.5。
来自另一个搜索结果。对我有用!
"您可以使用Visual Studio 2010,并且它支持它,前提是您的操作系统支持.NET 4.5。
右键单击解决方案以添加引用(与您通常做的方式相同)。当对话框出现时,请选择浏览,然后导航到以下文件夹:
C:\Program Files(x86)\Reference Assemblies\Microsoft\Framework\.Net Framework\4.5
您将在那里找到它。"
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- choose the fx target framework sub version, here 4.5.2 -->
<FrameworkVersion4FX>5</FrameworkVersion4FX>
<FrameworkVersion4FXX Condition="$(FrameworkVersion4FX) != ''">2</FrameworkVersion4FXX>
<TargetFrameworkVersionNET4FX>v4</TargetFrameworkVersionNET4FX>
<TargetFrameworkVersionNET4FX Condition="$(FrameworkVersion4FX) != '' ">$(TargetFrameworkVersionNET4FX).$(FrameworkVersion4FX)</TargetFrameworkVersionNET4FX>
<TargetFrameworkVersionNET4FX Condition="$(FrameworkVersion4FXX) != '' ">$(TargetFrameworkVersionNET4FX).$(FrameworkVersion4FXX)</TargetFrameworkVersionNET4FX>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'DebugNET4FX'">
<TargetFrameworkVersion>$(TargetFrameworkVersionNET4FX)</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'ReleaseNET4FX'">
<TargetFrameworkVersion>$(TargetFrameworkVersionNET4FX)</TargetFrameworkVersion>
</PropertyGroup>
</Project>