将解决方案中的所有项目重定向到 .NET 4.5.2

Retargeting All Projects in a Solution to .NET 4.5.2

我在 Visual Studio 2012 年有一个解决方案,其中包含 170 个 C# 项目。我需要将所有项目从 .NET Framework 4.0 重新定位到 4.5.2。

我更愿意让 Visual Studio 通过进入每个项目的属性、更改目标框架并让 Visual Studio 对 .csproj 文件进行任何必要的更改来处理此问题。

我注意到这些更改包括向 .csproj 添加一些新的 XML 标记,具体取决于当前项目的某些属性。

如何在不使用替换文本工具替换目标版本号的情况下批量重定向所有 170 个 C# 项目?我希望 Visual Studio 进行所有必要的标签修改和添加,而单独替换不会允许这种情况发生。

MSDN 文档“Migration Guide to the .NET Framework 4.5" and "How to Configure an App to Support .NET Framework 4 or 4.5”仅讨论修改项目。没有关于一次将更改应用于整个解决方案的详细信息,我也没有在 VS 中看到支持它的功能。

但是,如果您有兴趣,有一个名为 Target Framework Migrator available in the Visual Studio gallery, which supports upgrading to 4.5.2 (as well as newer versions**) and looks like it'll do exactly what you want. The source code is available on GitHub 的(评价很高的)扩展程序。

请注意,缺少此类功能可能是有意为之(而不仅仅是遗漏)。我只是在猜测,但也许 MS 认为只有 需要 新框架的项目才会升级。 FWIW,如果您最终升级了一些与其他解决方案共享的项目,这些解决方案可能无法构建,直到 它们也被 升级。

话虽这么说,如果您在一家只有一个(或几个)解决方案的小商店中,并且希望一次升级所有内容,那么上述工具也许适合您。


There's been no development on this for years, and apparently the developer has no plans to pass the baton to anyone else.

如果您无法使其与较新的 .NET Framework 版本一起使用,请检查现有的 PRs and Issues for fixes, but you may have to apply them yourself. For example, someone posted a fix for .NET Framework v 4.7.1。希望这些会被合并,但我不会屏住呼吸。

如果其他人看到与 Anas 相同的错误(在评论中),here's a GitHub issue from a couple weeks ago, and another possibly related issue 从 2017 年开始。如果您遇到相同的问题,请考虑对他们竖起大拇指并添加更多详细信息。

我自己构建了一个简单的工具来迁移整个解决方案的目标框架版本,因为目标框架迁移器扩展不支持 Visual Studio 2017。从我的 GitHub 存储库下载该工具https://github.com/Xpitfire/TargetFrameworkMigrator

我知道这不是最好的方法,但它对我有用,也许它也会帮助其他人。

由于 Target Framework Migrator 已损坏,我推出了自己的 search/replace(使用 git bash,它在 windows) ;基本上它将 v4.6.x 更改为 v4.7.2,然后将文件转换回使用臭名昭著的 DOS 的 CRLF :

find . \( -iname '*.csproj' -o -iname '*.vcxproj' -o -iname 'app.config' \) \
 -exec grep -Z -l 'v4\.6\..' \{} \; | xargs -0 sed -i 's/v4\.6\../v4.7.2/'  
find . \( -iname '*.csproj' -o -iname '*.vcxproj' -o -iname 'app.config' \) \
 -exec grep -Z -l 'v4\.7\..' \{} \; | xargs -0 unix2dos
public void ChangeFramework() {

  //Add Reference to envdte (Assemblies\Extensions\envDTE)
  string SolutionFile = @"C:\MyProject\MyProject.sln";
  string ProjectName = "MyProject";

  //------------------------------------------------------------------------
  //Find the Program ID from the registry for VisualStudio.DTE
  //Look it up In Registry: Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Classes
  System.Type oType = System.Type.GetTypeFromProgID("VisualStudio.DTE", true);
  EnvDTE.DTE dte = (EnvDTE.DTE)Activator.CreateInstance(oType, true);

  //------------------------------------------------------------------------
  //Open your Solution
  dte.Solution.Open(SolutionFile);

  //------------------------------------------------------------------------
  //Now In your solution go through what is listed in dte.Solution.Projects 
  //and find the one that match what you want to change target for
  int iItemsCount = dte.Solution.Projects.Count;
  string sCurrent = "";

  for (int i = 1; i <= iItemsCount; i++) {

    sCurrent = dte.Solution.Projects.Item(i).Name;

    if (dte.Solution.Projects.Item(i).Name == ProjectName) {
      //Once you find your project, Change the Framework
      EnvDTE.Project oProject = dte.Solution.Projects.Item(i);
      oProject.Properties.Item("TargetFrameworkMoniker").Value = ".NETFramework,Version = v4.6.2";
    }
  }

  //------------------------------------------------------------------------
  //Close your Solution
  dte.Solution.Close();
}

对于 .NET Framework 解决方案,一个简单的“在文件中替换”对我有用:

例如:从.NET Framework 4.5.2.NET Framework 4.7.2

package.config个文件中,替换所有

targetFramework="net452" 

targetFramework="net472" 

*.csproj文件中,替换所有

<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> 

<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>

Target Framework Migrator 非常有用。默认情况下,它达到 v4.7。但是,添加对 v4.7.1、v4.7.2 和 v4.8 的支持很容易。

在 C:\Users{用户名}\AppData\Local\Microsoft\VisualStudio\ 文件夹中找到 Frameworks.xml 文件并通过添加以下框架版本进行编辑:

<Framework Id="262152" Name=".NETFramework,Version=v4.8"/>
<Framework Id="262663" Name=".NETFramework,Version=v4.7.2"/>
<Framework Id="262407" Name=".NETFramework,Version=v4.7.1"/>

重新启动后 visual studio,您将看到新版本。