How do I programmatically list all of the projects in a solution?
in solution, there many projects. windows service projects , console projects. want create new project list windows service name in solution.
thanks.
because have difficulty use dte, use code stackoverflow.
public class solution { //internal class solutionparser //name: microsoft.build.construction.solutionparser //assembly: microsoft.build, version=4.0.0.0 static readonly type s_solutionparser; static readonly propertyinfo s_solutionparser_solutionreader; static readonly methodinfo s_solutionparser_parsesolution; static readonly propertyinfo s_solutionparser_projects; static solution() { s_solutionparser = type.gettype("microsoft.build.construction.solutionparser, microsoft.build, version=4.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a", false, false); if (s_solutionparser != null) { s_solutionparser_solutionreader = s_solutionparser.getproperty("solutionreader", bindingflags.nonpublic | bindingflags.instance); s_solutionparser_projects = s_solutionparser.getproperty("projects", bindingflags.nonpublic | bindingflags.instance); s_solutionparser_parsesolution = s_solutionparser.getmethod("parsesolution", bindingflags.nonpublic | bindingflags.instance); } } public list<solutionproject> projects { get; private set; } public solution(string solutionfilename) { if (s_solutionparser == null) { throw new invalidoperationexception("can not find type 'microsoft.build.construction.solutionparser' missing assembly reference 'microsoft.build.dll'?"); } var solutionparser = s_solutionparser.getconstructors(bindingflags.instance | bindingflags.nonpublic).first().invoke(null); using (var streamreader = new streamreader(solutionfilename)) { s_solutionparser_solutionreader.setvalue(solutionparser, streamreader, null); s_solutionparser_parsesolution.invoke(solutionparser, null); } var projects = new list<solutionproject>(); var array = (array)s_solutionparser_projects.getvalue(solutionparser, null); (int = 0; < array.length; i++) { projects.add(new solutionproject(array.getvalue(i))); } this.projects = projects; } } [debuggerdisplay("{projectname}, {relativepath}, {projectguid}")] public class solutionproject { static readonly type s_projectinsolution; static readonly propertyinfo s_projectinsolution_projectname; static readonly propertyinfo s_projectinsolution_relativepath; static readonly propertyinfo s_projectinsolution_projectguid; static solutionproject() { s_projectinsolution = type.gettype("microsoft.build.construction.projectinsolution, microsoft.build, version=4.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a", false, false); if (s_projectinsolution != null) { s_projectinsolution_projectname = s_projectinsolution.getproperty("projectname", bindingflags.nonpublic | bindingflags.instance); s_projectinsolution_relativepath = s_projectinsolution.getproperty("relativepath", bindingflags.nonpublic | bindingflags.instance); s_projectinsolution_projectguid = s_projectinsolution.getproperty("projectguid", bindingflags.nonpublic | bindingflags.instance); } } public string projectname { get; private set; } public string relativepath { get; private set; } public string projectguid { get; private set; } public solutionproject(object solutionproject) { this.projectname = s_projectinsolution_projectname.getvalue(solutionproject, null) string; this.relativepath = s_projectinsolution_relativepath.getvalue(solutionproject, null) string; this.projectguid = s_projectinsolution_projectguid.getvalue(solutionproject, null) string; } }
then call
solution s = new solution("mysolution.sln");
how can solution path because current directory not in solution folder?
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment