How can I progrommatically change the target framework from 4.0 to 3.5 of a project/solution?

Posted by scott on Stack Overflow See other posts from Stack Overflow or by scott
Published on 2011-01-10T14:55:45Z Indexed on 2011/01/11 14:53 UTC
Read the original article Hit count: 211

Filed under:
|

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.
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?

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.

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.

Edit 2: After a lot of googling I found this. so then I tried this:

loProp = vsGetProperty("TargetFrameworkMoniker");
vsSetValue(loProp, ".NETFramework,Version=v3.5");

the definitions for those two methods are below. as far as I can tell they do the same this as

project.Properties.Item("TargetFrameworkMoniker").Value =   ".NETFramework,Version=v4.0,Profile=Client";

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.

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.

here is vsGetProperty

    protected Property vsGetProperty(string aProperty)
    {
        bool lbDone = false;
        int liCount = 0;
        Property loProp;
        while (!lbDone && liCount < 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;
    }

and vsSetValue

    protected void vsSetValue(Property aProperty, string aValue)
    {
        bool lbDone = false;
        int liCount = 0;
        while (!lbDone && liCount < 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);
                }
            }
        }
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about code-generation