我在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”和“How to Configure an App to Support .NET Framework 4 or 4.5”仅讨论修改项目。没有关于立即对整个解决方案应用更改的详细信息,也没有在VS中看到支持它的功能。
但是,在Visual Studio库中有一个名为Target Framework Migrator的(评级很高的)扩展,它支持升级到4.5.2(以及更新的版本**),看起来它可以完全按照你的意愿行事。如果您有兴趣,可以在GitHub上找到源代码。
请注意,缺少这样的功能可能是故意的(而不仅仅是遗漏)。我只是猜测,但也许MS只计算需要新框架的项目才会升级。 FWIW,如果你最终升级了一些与其他解决方案共享的项目,那么这些解决方案在升级之前可能无法构建。
话虽这么说,如果你在一个只有一个(或几个)解决方案的小商店,并且你想要一次性升级所有东西,那么上述工具也许适合你。
**最近没有太多的发展。如果您无法使用较新的.NET Framework版本,请检查现有的PRs和Issues以获取修复。例如,某人发布了fix for .NET Framework v 4.7.1。希望这些最终会合并。
我已经构建了一个简单的工具来迁移整个解决方案的目标框架版本,因为Target Framework Migrator Extension不支持Visual Studio 2017.从我的GitHub存储库下载该工具https://github.com/Xpitfire/TargetFrameworkMigrator
我知道这不是最好的方式,但它对我有用,也许它也会帮助别人。
由于Target Framework Migrator已损坏,我自行搜索/替换(使用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();
}