通过Visual Studio 2010定位.NET Framework 4.5

Gol*_*ius 165 visual-studio-2010 visual-studio .net-4.5

今天我在我的机器上安装了.NET Framework 4.5,希望能够在Visual Studio 2010中使用它,因为它只是一个小的更新,不应该给Visual Studio 2010带来问题.不幸的是我不是,甚至手动删除某些4.0并添加相应的4.5程序集导致原始4.0程序集仍在项目中引用.

是否可以从Visual Studio 2010中定位4.5版,如果是,如何?我真的很想用丝带......

小智 192

Visual Studio 2010之前的每个Visual Studio版本都与特定的.NET框架相关联.(VS2008是.NET 3.5,VS2005是.NET 2.0,VS2003是.NET1.1)Visual Studio 2010及更高版本允许定位先前的框架版本,但不能用于将来的版本.您必须使用Visual Studio 2012才能使用.NET 4.5.

  • 不完全正确 - 在VS版本之间发布了.NET 3.0,可以在VS2005中使用. (9认同)
  • 真是个难题:迁移到vs2012 +以利用.net 4.5的精确度,代价是失去了心爱的vs2010安装项目以部署msi安装程序......"升级"感觉更像是一个侧面...... (9认同)
  • @ecoe - 有一个VS 2013扩展,它带回了安装和部署项目类型:https://visualstudiogallery.msdn.microsoft.com/9abe329c-9bba-44a1-be59-0fbf6151054d (6认同)
  • 这是不正确的.您无需升级即可使用此功能.您需要做的就是添加一个新引用,然后浏览到.NET 4.5文件夹:C:\ Program Files(x86)\ Reference Assemblies\Microsoft\Framework \\.Net Framework\4.5.你会在那里找到参考.只需添加它就可以了 (2认同)
  • 在VS 2010中@HermesTrismegistus点击"目标框架=安装其他框架",它将我带到一个网站https://www.microsoft.com/net/download/visual-studio-sdks它不允许我导航到任何文件夹.我该怎么办.我必须能够从VS 2010中定位4.5.Tks. (2认同)

小智 67

有一些相当有限的场景,我可以想到这将有用,但我们假设您无法获得资金购买VS2012或其他类似的东西.如果是这种情况并且您拥有Windows 7+和VS 2010,那么您可以使用我放在一起的以下hack似乎可行(但我尚未使用此方法完全部署应用程序).

  1. 备份你的项目文件!

  2. 下载并安装包含.NET 4.5 SDK 的Windows 8 SDK.

  3. 在VS2010中打开您的项目.

  4. 在项目中创建一个文本文件,其名称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>
    
    Run Code Online (Sandbox Code Playgroud)
  5. 卸载您的项目(右键单击 - >卸载).

  6. 编辑项目文件(右键单击 - >编辑*.csproj).

  7. 在项目文件中进行以下更改:

    一个.将默认值替换为Microsoft.CSharp.targets在步骤4中创建的目标文件

    <!-- Old Import Entry -->
    <!-- <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> -->
    
    <!-- New Import Entry -->
    <Import Project="Compile_4_5_CSharp.targets" />
    
    Run Code Online (Sandbox Code Playgroud)

    湾 将默认平台更改为.NET 4.5

    <!-- Old default platform entry -->
    <!-- <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> -->
    
    <!-- New default platform entry -->
    <Platform Condition=" '$(Platform)' == '' ">.NET 4.5</Platform>
    
    Run Code Online (Sandbox Code Playgroud)

    C.添加AnyCPU平台以允许定位项目属性中指定的其他框架.这应该<ItemGroup>在文件中的第一个标记之前添加

    <PropertyGroup Condition="'$(Platform)' == 'AnyCPU'">
        <PlatformTarget>AnyCPU</PlatformTarget>
    </PropertyGroup>
    
    .
    .
    .
    <ItemGroup>
    .
    .
    .
    
    Run Code Online (Sandbox Code Playgroud)
  8. 保存更改并关闭*.csproj文件.

  9. 重新加载项目(右键单击 - >重新加载项目).

  10. 在配置管理器(Build - > Configuration Manager)中,确保为项目选择了".NET 4.5"平台.

  11. 仍然在配置管理器中,为".NET 4.5"创建一个新的解决方案平台(您可以将其基于"任何CPU"),并确保为该解决方案选择".NET 4.5".

  12. 构建项目并检查错误.

  13. 假设构建完成,您可以通过在源代码中添加对4.5特定类的引用来验证您确实以4.5为目标:

    using System;
    using System.Text;
    
    namespace testing
    {
        using net45check = System.Reflection.ReflectionContext;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  14. 使用".NET 4.5"平台进行编译时,构建应该会成功.当您在"任何CPU"平台下编译时,您应该收到编译器错误:

    Error 6: The type or namespace name 'ReflectionContext' does not exist in
    the namespace 'System.Reflection' (are you missing an assembly reference?)
    
    Run Code Online (Sandbox Code Playgroud)

  • 这是krazy,即使它工作,像丝带设计师的UI元素工作没有大量的xaml和其他错误?srsly只是升级. (3认同)

cyb*_*rgy 8

仅供参考,如果你想在VS2010中创建一个安装程序包,不幸的是它只针对.NET 4.要解决这个问题,你必须添加NET 4.5作为启动条件.

将以下内容添加到安装程序的启动条件中(右键单击,查看,启动条件).

在"搜索目标计算机"中,右键单击并选择"添加注册表搜索".

Property: REGISTRYVALUE1
RegKey: Software\Microsoft\NET Framework Setup\NDP\v4\Full
Root: vsdrrHKLM
Value: Release
Run Code Online (Sandbox Code Playgroud)

添加新的"启动条件":

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.
Run Code Online (Sandbox Code Playgroud)

哪里:

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