{"id":2443,"date":"2010-02-25T13:00:00","date_gmt":"2010-02-25T13:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudio\/2010\/02\/25\/how-to-retarget-a-project-using-dte\/"},"modified":"2019-02-14T15:39:48","modified_gmt":"2019-02-14T23:39:48","slug":"how-to-retarget-a-project-using-dte","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/how-to-retarget-a-project-using-dte\/","title":{"rendered":"How to: Retarget a project using DTE"},"content":{"rendered":"<p>The upcoming .NET Framework 4 and Visual Studio 2010 (now in RC) has a lot of exciting new features. From BigInt and parallel libraries, to code contracts, CLR side by side, a new project system for C++ and multi-targeting. Just to name a few. There is much more. No matter what features you are the most interested in, you can start exploring. <\/p>\n<p><span><strong>Conversion and retargeting <\/strong><\/span><\/p>\n<p>If you have a Whidbey or Orcas project and try opening it in Visual Studio 2010, the conversion wizard will do all the necessary steps to make sure your project is usable in the new installment. You can start by opening your project in Visual Studio 2010: <\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/02\/021910_0122_HowtoRetarg1.png\"> <\/p>\n<p>The conversion wizard will popup and it will guide you step by step until the conversion is complete. When the conversion is done, the project will be loaded in Visual Studio. The conversion wizard may also ask you to retarget the project to .NET Framework 4, if you don&#8217;t have the version of the framework your project is targeting installed. <\/p>\n<p>After your project is loaded, you can then retarget to .NET Framework 4 or other version, from the project properties: <\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/02\/021910_0122_HowtoRetarg2.png\"> <\/p>\n<p>Project retargeting is a quite complex procedure, and goes beyond modifying the project file: <\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/02\/021910_0122_HowtoRetarg3.png\"> <\/p>\n<p>A retarget also unloads the project, may update the web.config and the app.config files (generating the latter if not on the disk), in the end to reload the project back in Visual Studio. That is why it can&#8217;t always be done by externally modifying the project file. <\/p>\n<p><span><strong>TFMs and Client Profiles <\/strong><\/span><\/p>\n<p>In the project file sample above, the 2 properties identify which version of the framework, and optionally the profile, a project is targeting. They are not a <em>target framework moniker<\/em> (TFM) but they are rendered from one. A TFM is an identifier which tells the framework family, the version and the profile. It is used extensively by Visual Studio, and some of the higher level consumers are: <\/p>\n<ul>\n<li>intellisense, to tell which types are available at runtime <\/li>\n<li>Add Reference dialog, which assemblies should be displayed to the user <\/li>\n<li>Toolbox, which controls to show <\/li>\n<li>MSBUILD, which&nbsp;references to pass to the compiler&nbsp;<\/li>\n<\/ul>\n<p>A TFM may also denote a profile of a full framework. Technically a profile is no different from a full framework. There is a deployment package which delivers it, assemblies which it comes with and a user can reference, and you can target the profile from the project property pages. The difference lies on what&#8217;s available for you to reference, that is why it is sometimes called a &#8220;subset&#8221;. Not all the assemblies available in the full framework are available in one of its profiles. As an example, System.Web is not in the .NET Framework 4 Client Profile: <\/p>\n<p>&nbsp;<img decoding=\"async\" align=\"middle\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/02\/ht_retarget_2_folders.jpg\" width=\"485\" height=\"350\"><\/p>\n<p>Profiles were designed to decrease the deployment cost and to allow users to extend an existing framework for particular needs. For example one can introduce a &#8220;Mono&#8221; profile and target it seamlessly in Visual Studio, or other popular Mono IDEs. A profile is also the default target for many project templates in Visual Studio 2010. <\/p>\n<p><span><strong>DTE retargeting <\/strong><\/span><\/p>\n<p>Say you have a bunch of projects in a solution, which were originally created in Orcas, and you want them to target .NET Framework 4. Is there an easy way other than manually going to the property pages and do it from the UI? With the new release of Visual Studio we are introducing a new DTE property which allows retargeting a project based on a TFM: <\/p>\n<p><span>project.Properties.Item(<span>&#8220;TargetFrameworkMoniker&#8221;<\/span>).Value = <span>&#8220;.NETFramework,Version=v4.0,Profile=Client&#8221;<\/span>; <\/span><\/p>\n<p>Assuming you have an existing project and already acquired a DTE instance, this code will load the project in Visual Studio 2010 then retarget it to .NET Framework 4 Client Profile:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/p>\n<p><span><span>\/\/ open an existing project<br \/><\/span><\/span><span>dte.Solution.Open(<span>@&#8221;D:Users&#8230;ProjectsConsoleApplication2ConsoleApplication2.sln&#8221;<\/span>);&nbsp;<\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/p>\n<p><span><span>\/\/ assumes the first item in the collection is the target project<\/span> <br \/><\/span><span><span>Project<\/span> project = dte.Solution.Projects.Item(1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/p>\n<p><span>\/\/ retarget the project to .NET Framework 4<span> <span>Client Profile<\/span> <br \/><\/span><\/span><span>project.Properties.Item(<span>&#8220;TargetFrameworkMoniker&#8221;<\/span>).Value = <\/span><span><span><span>new<\/span> <span>FrameworkName<\/span>(<span>&#8220;.NETFramework&#8221;<\/span>, <span>new<\/span> <span>Version<\/span>(4, 0),<span> Client&#8221;<\/span>).FullName;<\/span><span> <\/span><\/span><\/p>\n<p>You can then integrate this further in an add-in or a macro, to fulfill your special purposes. In Visual Studio 2010, go to Tools \u2013 Macros \u2013 Macros IDE. In the Project Explorer expand My Macros and in Module1 add this code: <\/p>\n<p><span><span>&#8216; macro to retarget all C#\/VB projects in a solution to .NET Framework 4<\/span> <br \/><\/span><span><span>Sub<\/span> SwitchFramework() <br \/><\/span><span><span>&nbsp;&nbsp;&nbsp; Dim<\/span> v40FrameworkName <span>As<\/span> FrameworkName = <span>New<\/span> FrameworkName(<span>&#8220;.NETFramework&#8221;<\/span>, <span>New<\/span> Version(4, 0)) <\/p>\n<p><\/span><span><span>&nbsp;&nbsp;&nbsp; For<\/span> <span>Each<\/span> project <span>As<\/span> EnvDTE.Project <span>In<\/span> DTE.Solution.Projects <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span><span>If<\/span> project.Kind = PrjKind.prjKindCSharpProject <span>OrElse<\/span>&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span>project.Kind = PrjKind.prjKindVBProject <span>Then&nbsp;<\/p>\n<p><\/span><\/span><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>project.Properties.Item(<span>&#8220;TargetFrameworkMoniker&#8221;<\/span>).Value = v40FrameworkName.FullName <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span><span>End<\/span> <span>If&nbsp;<br \/><\/span><\/span><span><span>&nbsp;&nbsp;&nbsp;&nbsp;Next <br \/><\/span><\/span><span><span>End<\/span> <span>Sub <\/span><\/span><\/p>\n<p>This will retarget all C# and VB projects from your solution to .NET Framework 4. <\/p>\n<p>I hope that you find this post useful, and let us know if you have any questions. <\/p>\n<table border=\"0\" cellSpacing=\"0\" cellPadding=\"2\">\n<tbody>\n<tr>\n<td vAlign=\"top\">&nbsp;<\/td>\n<p>&lt;td =&quot;&#013;&#010;&#013;&#010;<\/p>\n<td><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/clip_image001_2.jpg\"><img decoding=\"async\" title=\"clip_image001\" border=\"0\" alt=\"clip_image001\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/02\/clip_image001_thumb.jpg\" width=\"111\" height=\"122\"><\/a><\/td>\n<td vAlign=\"top\">\n<p>Ion Todirel, Visual Studio: Project and Build<\/p>\n<p><em>&#8220;Hi, I\u2019m Ion and you may know me from Channel 9. I\u2019m a test dev in the Project and Build team. I work mainly on multitargeting, references and other design-time features.&#8221;<\/em><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>The upcoming .NET Framework 4 and Visual Studio 2010 (now in RC) has a lot of exciting new features. From BigInt and parallel libraries, to code contracts, CLR side by side, a new project system for C++ and multi-targeting. Just to name a few. There is much more. No matter what features you are the [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":255385,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[155],"tags":[1383],"class_list":["post-2443","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visual-studio","tag-c"],"acf":[],"blog_post_summary":"<p>The upcoming .NET Framework 4 and Visual Studio 2010 (now in RC) has a lot of exciting new features. From BigInt and parallel libraries, to code contracts, CLR side by side, a new project system for C++ and multi-targeting. Just to name a few. There is much more. No matter what features you are the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/2443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/comments?post=2443"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/2443\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media\/255385"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media?parent=2443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/categories?post=2443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/tags?post=2443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}