title: How can I progrommatically change the target framework from 4.0 to 3.5 of a project/solution?
body: <p>Edit 3:
  After more googling it looks like you can't have the TargetFrameworkMoniker property in a .NET 3.5 application. So I guess I should be asking a different question.<br>
  How do I change the Target framework from 4.0 to 3.5? Unfortunately, I can only find stuff on how to go the other way. or better yet how do i progrommatically set the target framework version of a project to something other than 4.0?</p>
  
  <p>Original question:
  I just switched to vs2010.  I have an application that uses .net 3.5.  It loads plugins which are generated by a different app.  The plugins are using .net 4 and there for cannot be loaded.  I'm using EnvDTE.Project to create a project and set the settings.  I can't find what setting needs to be set for this.</p>
  
  <p>Edit 1:
  I'm generating code for about 50 solutions.  When I made the switch from vs2005 to vs2010 the projects in those solutions are defaulting to .NET Framework 4.0.  So I need to set the .NET Framework to 3.5 when I am generating the code for these solutions.</p>
  
  <p>Edit 2:
  After a lot of googling I found <a href="http://blogs.msdn.com/b/visualstudio/archive/2010/02/25/how-to-retarget-a-project-using-dte.aspx" rel="nofollow">this</a>. so then I tried this:</p>
  
  <pre><code>loProp = vsGetProperty("TargetFrameworkMoniker");
  vsSetValue(loProp, ".NETFramework,Version=v3.5");
  </code></pre>
  
  <p>the definitions for those two methods are below. as far as I can tell they do the same this as </p>
  
  <pre><code>project.Properties.Item("TargetFrameworkMoniker").Value =   ".NETFramework,Version=v4.0,Profile=Client";
  </code></pre>
  
  <p>I start getting an Property Unavailable Exception later in the code.  When I remove the new lines everything works except the projects target framework is still 4.0.</p>
  
  <p>The code generators target framework is 3.5 so I can't use the FrameworkName class like shown in the second example in that link.</p>
  
  <p>here is vsGetProperty</p>
  
  <pre><code>    protected Property vsGetProperty(string aProperty)
      {
          bool lbDone = false;
          int liCount = 0;
          Property loProp;
          while (!lbDone &amp;&amp; liCount &lt; pMaxRetries)
          {
              try
              {
                  loProp = pProject.Properties.Item(aProperty);
                  lbDone = true;
                  return loProp;
              }
              catch (System.Runtime.InteropServices.COMException loE)
              {
                  liCount++;
                  if ((uint)loE.ErrorCode == 0x80010001)
                  {
                      // RPC_E_CALL_REJECTED - sleep half sec then try again
                      System.Threading.Thread.Sleep(pDelayBetweenRetry);
                  }
              }
          }
          return null;
      }
  </code></pre>
  
  <p>and vsSetValue</p>
  
  <pre><code>    protected void vsSetValue(Property aProperty, string aValue)
      {
          bool lbDone = false;
          int liCount = 0;
          while (!lbDone &amp;&amp; liCount &lt; pMaxRetries)
          {
              try
              {
                  aProperty.Value = aValue;
                  lbDone = true;
              }
              catch (System.Runtime.InteropServices.COMException loE)
              {
                  liCount++;
                  if ((uint)loE.ErrorCode == 0x80010001)
                  {
                      // RPC_E_CALL_REJECTED - sleep half sec then try again
                      System.Threading.Thread.Sleep(pDelayBetweenRetry);
                  }
              }
          }
      }
  </code></pre>