在.NET 4.5.2解决方案中重定向所有项目

我在 Visual Studio 2012中有一个解决方案,里面有170个 C # 项目。我需要重新定位。NET Framework 4.0到4.5.2。

我倾向于让 VisualStudio 通过进入每个项目的属性、更改目标框架以及让 VisualStudio 对。Csproj 文件。

我注意到这些更改包括向。Csproj,这取决于当前项目的一些属性。

如何在不使用替换文本工具替换目标版本号的情况下批处理所有170个 C # 项目?我希望 VisualStudio 进行所有必要的标记修改和添加,并单独替换将不允许这种情况发生。

87417 次浏览

The MSDN documentation "Migration Guide to the .NET Framework 4.5" and "How to Configure an App to Support .NET Framework 4 or 4.5" only discusses modifying projects. There's no details on applying changes to the entire solution at once, nor have I seen a function in VS that supports it.

However, there's a (well-rated) extension called 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, if you're interested.

Note that the lack of such a feature may be intentional (and not just an omission). I'm just guessing, but maybe MS figures only projects that need the new Frameworks will be upgraded. FWIW, if you end up upgrading some projects that are shared with other solutions, those solutions may fail to build until they're upgraded too.

That being said, if you're in a small shop with just one (or a few) solutions and you're looking to upgrade everything in one go, then perhaps the above tool will work for you.


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

If you're unable to get it to work with a newer .NET Framework version, check the existing 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. Hopefully these will get merged, but I wouldn't hold my breath.

If anyone else is seeing the same error as Anas (in the comments), here's a GitHub issue from a couple weeks ago, and another possibly related issue from 2017. Consider thumbs upping them and adding more details if you're having the same problem.

I have built myself a simple tool to migrate the target framework versions for an entire solution, because the Target Framework Migrator Extension does not support Visual Studio 2017. Download the tool from my GitHub repository https://github.com/Xpitfire/TargetFrameworkMigrator

I know this is not the best way to go, but it worked for me and maybe it will also help someone else.

Since the Target Framework Migrator is broken, I rolled my own search/replace (using git bash, it works ok on windows) ; Basically it changes the v4.6.x into v4.7.2, then it converts back the files to using the infamous DOS's 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();
}

For a .NET Framework solution, a simple "Replace in files" did the trick for me:

eg: From .NET Framework 4.5.2 to .NET Framework 4.7.2

In package.config files, replace all

targetFramework="net452"

to

targetFramework="net472"

In *.csproj files, replace all

<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>

to

<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>

Target Framework Migrator is pretty useful. By default, it comes up to v4.7. However, it's easy to add support for v4.7.1, v4.7.2 and v4.8.

Find Frameworks.xml file in C:\Users{username}\AppData\Local\Microsoft\VisualStudio\ folder and edit by adding these framework versions:

<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"/>

After you restart visual studio, you will see new versions.