Geeks With Blogs

News



Microsoft Store

Support This Site


AddThis Social Bookmark Button

Locations of visitors to this page

Subscribers to this feed

TwitterCounter for @sdorman

Creative Commons License


Scott Dorman ephemeral segment

A question was asked yesterday in one of the CodeProject discussion forums by someone looking for a way to easily change the Target Framework version of all projects in a solution. If you don’t know, the Target Framework version is what tells the compiler which version of the .NET Framework to compile against (more information is available here) and can be set to one of the following values:

  • .NET Framework 2.0
  • .NET Framework 3.0
  • .NET Framework 3.5

This can be easily accomplished by editing the project properties:

image

The problem with this approach is that if you need to change a lot of projects at one time it becomes rather unwieldy. (In this particular case, there were 56 projects that need to be changed). One possible solution is to edit the project files by hand in a text editor and change the <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> property to the correct value. Again, this is not only time consuming but can also be error-prone.

The better solution is to automate this through the use of a Visual Studio macro. Since this looked like an interesting exercise and was a chance to dig a bit deeper into the strange (and largely undocumented) world of macros, I created one that does just that. It prompts you for the target framework version you want to set for all of the projects and then loops through each project in the solution and makes the change. It is smart enough to skip project types that don’t support this property and projects that are already at the correct version.

The macro is available on my SkyDrive account. Download it to your <UserProfile>\Documents\Visual Studio 2008\Projects\VSMacros80\MyMacros folder, open the Visual Studio Macro IDE (Alt-F11) and add it as an existing item to the “MyMacros” project.

I make no guarantees or warranties on this macro. I have tested it on several solutions and projects and everything seems to work and not cause any problems, but, as always, use with caution. Since it is a macro, you have the full source code available to investigate and see what it’s actually doing. If you find any bugs or make any useful changes, please let me know and I’ll update the macro.

Technorati Tags: ,
Posted on Friday, July 18, 2008 12:36 PM .NET (General) | Back to top


Comments on this post: Visual Studio 2008 and TargetFrameworkVersion

# re: Visual Studio 2008 and TargetFrameworkVersion
Requesting Gravatar...
Thanks for the macro Scott. I made a couple of changes to it, specifically to the ChageTargetFrameworkForAllProjects() and ChangeTargetFramework() methods so that the macro is applied to projects that are nested many levels deep. I've pasted the code for those 2 methods below in case you want to include it in your file.


'' Change the target framework for all projects in the current solution.
Sub ChangeTargetFrameworkForAllProjects()
Dim project As EnvDTE.Project
Write("--------- CHANGING TARGET .NET FRAMEWORK VERSION -------------")
Try
If Not DTE.Solution.IsOpen Then
Write("There is no solution open.")
Else
Dim targetFrameworkInput As String = InputBox("Enter the target framework version (Fx35, Fx30, Fx20):", "Target Framework", "Fx35")
Dim targetFramework As TargetFramework = [Enum].Parse(GetType(TargetFramework), targetFrameworkInput)
For Each project In DTE.Solution.Projects
ChangeTargetFramework(project, targetFramework)
Next
End If
Catch ex As System.Exception
Write(ex.Message)
End Try
End Sub

'' Change the target framework for a project.
Function ChangeTargetFramework(ByVal project As EnvDTE.Project, ByVal targetFramework As TargetFramework) As Boolean
Dim changed As Boolean = True

If project.Kind = Constants.vsProjectKindSolutionItems Or project.Kind = Constants.vsProjectKindMisc Then
For Each projectItem In project.ProjectItems
If Not (projectItem.SubProject Is Nothing) Then
ChangeTargetFramework(projectItem.SubProject, targetFramework)
End If
Next
Else
Try
If IsLegalProjectType(project) Then
SetTargetFramework(project, targetFramework)
Else
Write("Skipping project: " + project.Name + " (" + project.Kind + ")")
End If
Catch ex As Exception
Write(ex.Message)
changed = False
End Try
End If

Return changed
End Function

Left by George Mathew on Oct 02, 2008 8:07 PM

# re: Visual Studio 2008 and TargetFrameworkVersion
Requesting Gravatar...
I am wondering what kind of project it is in your demo picture, thanks.
Left by ngl on Aug 31, 2011 4:11 AM

Your comment:
 (will show your gravatar)
 


Copyright © Scott Dorman | Powered by: GeeksWithBlogs.net | Join free