Create a solution and add a project using “VisualS

2019-05-20 09:29发布

问题:

I'm trying to create a VS2010 solution and add a project from a stand-alone app (not an add-in). I can create an instance of VS2010, but I'm not able to determine how to create a project properly...I can only find an example of how to create a project using the EnvDTE80 object, which later causes an exception because the project file is in an earlier format and needs to be upgraded. I have this:

EnvDTE80.DTE2 dte2;
object obj;
System.Type t;
t = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0", true);
obj = System.Activator.CreateInstance(t, true);
dte2 = (EnvDTE80.DTE2)obj;

What I'm looking for is the equivalent of something like "EnvDTE100.DTE2" but don't know how to get there.

Thanks

回答1:

You do not have to go via DTE object. The treatment to the object solution4 it's different you should do this

Type latestSolution = Type.GetTypeFromProgID("VisualStudio.10.0", true);
EnvDTE100.Solution4 vsSolution = (EnvDTE100.Solution4)Activator.CreateInstance(latestSolution, true);


回答2:

I think I'm doing something similar, I have a application that creates a solution and loads two projects from templates that I created in VS2010. You're right in that it seems everything still uses the EnvDTE80, even in VS2010, but then we use it to create a 2010 solution:

System.Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
Object obj = System.Activator.CreateInstance(type, true);
EnvDTE80.DTE2 dte2 = (EnvDTE80.DTE2)obj;
EnvDTE100.Solution4 soln = (EnvDTE100.Solution4)dte2.Solution;

Then you can call methods on the soln object to create your project (in my case its AddFromTemplate).