# Remove projects from a solution programmatically

• ### Question

• I'm writing a console application to deploy a test environment to test a big software (a hundred projects for a total of more than 1gb of code) for my company.

This script get the latest version of the software from tfs, build it and publish on local folders...

But i need to exclude some projects from the automated build and i found out that the only way to achieve this is by removing them from solution.

I'm trying to open my solution with EnvDTE100 to try this answer but I can't even find out how to use the new DTE and there's no useful documentantion on msdn (as usual)

my code is

System.Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.11.0");
Object obj = System.Activator.CreateInstance(type, true);
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)obj;

dte.Solution.Open(SlnPath);

but it get stuck on the .Open for ever (i use EnvDTE80 because EnvDTE100 doesn't have .DTE2)

I also tried with

Type latestSolution = Type.GetTypeFromProgID("VisualStudio.DTE.11.0", true);
EnvDTE100.Solution4 sln = (EnvDTE100.Solution4)Activator.CreateInstance(latestSolution, true);
sln.Open(SlnPath);

but on the second line I get

Unable to cast COM object of type 'System.__ComObject' to interface type 'EnvDTE100.Solution4'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{CDA7305C-78B6-4D9D-90AD-93EBE71F9341}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

• Moved by Wednesday, March 12, 2014 10:03 AM
Tuesday, March 11, 2014 4:14 PM

• You're using the wrong configuration in the MSBuild command line.  You are specifying Debug as the configuration but you're solution configuration (based upon your screenshot) is called testConfiguration.  Adjust your build command line and try again.

msbuild.exe mysolution.sln /t:Build /p:Configuration=testConfiguration;Platform="Any CPU"

Friday, March 14, 2014 2:27 PM

### All replies

• Hi,

I moved this case to C# forum for better response.

Moreover, I suggest you read the documents below first.

_Solution.Open Method

http://msdn.microsoft.com/en-us/library/envdte._solution.open.aspx

How to: Get References to the DTE and DTE2 Objects

Sunny

Wednesday, March 12, 2014 10:02 AM
• Hello,

I would like you to check these two articles:

HOWTO: Automating Visual Studio .NET from outside the IDE

How to start Visual Studio programmatically

I follow the steps in these two articles and succeeded to open my solution, please check my sample code(I'm using Visual Studio 2013, so the library name is "VisualStudio.DTE.12.0" ),

Type visualStudioType = Type.GetTypeFromProgID("VisualStudio.DTE.12.0");
DTE dte = Activator.CreateInstance(visualStudioType) as DTE;
dte.MainWindow.Visible = true;

EnvDTE80.DTE2 dte2 = (EnvDTE80.DTE2)
System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.12.0");

dte2.Solution.Open(@"C:\Users\username\Documents\visual studio 2013\Projects\ConsoleApplication1\ConsoleApplication1.sln");

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

Wednesday, March 12, 2014 12:30 PM
• it works, but on the opening i get a bunch of messagebox (iis, source control, etc.) which i'd like to avoid...

Wednesday, March 12, 2014 2:11 PM
• I wouldn't recommend that you modify the solution as that requires code changes at build time which is generally frowned upon.  Is there any reason why you cannot set up a solution configuration specifically for your TFS build that only builds the projects you need to build?  This solution configuration would only be used by TFS builds while devs would continue to use the normal Dev/Release build.

Michael Taylor
http://msmvps.com/blogs/p3net

Wednesday, March 12, 2014 5:32 PM
• yes, i've created a new build configuration named "test" in which i excluded some projects from build.

now my build script is

public static bool BuildSolution()
{
bool result = false;
try
{
string solutionPath = @"C:\path\to\my\solution.sln";

ProjectCollection pc = new ProjectCollection();

Dictionary<string, string> GlobalProperty = new Dictionary<string, string>();

BuildParameters bp = new BuildParameters(pc);
BuildRequestData BuidlRequest = new BuildRequestData(solutionPath, GlobalProperty, "4.0", new string[] { "Build" },null);
BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp, BuidlRequest);

if (buildResult.OverallResult == BuildResultCode.Success)
{
result = true;
}
}
catch (Exception e)
{
Console.WriteLine("Build Failed: " + e.ToString());
}
return result;
}

but it works only if i manually remove those projects from the solution...

as if i created the new build configuration for nothing, and i can't understand why...

also, when the build fails, buildResult.Exception is null and i don't know how to find out what if failed for

Thursday, March 13, 2014 8:32 AM
• Programmatically creating a configuration isn't the solution nor is manually removing the projects.  It sounds like you have an issue with your solution and TFS. Please add the configuration to your solution normally and then exclude (don't unload) the projects from the configuration. Then look at the build logs to identify what is failing and let us know.
Thursday, March 13, 2014 12:48 PM
• i've never talk about creating a configuration programmatically.

i have a solution with 100+ project on tfs.

three of them are under development and do not compile, but the rest of the solution does.

i download the whole solution source from tfs programmatically and i need to build all the solution but those 3 projects.

i MANUALLY created a "test" build configuration excluding those projects and then added

GlobalProperty.Add("Configuration", "test");

to my build script, but it fails anyway.

then, for testing purpose, i manually removed those 3 project from solution and tried to launch my build script again, and it successed.

so here's my question: how can i programmatically download and build my solution excluding those 3 projects?

possible solutions i found:

1)using envdte dll -> open solution from code and remove those projects -> on solution opening i get tons of messageboxes (source control, iss, etc.)

2)using a custom build configuration MANUALLY created and committed to tfs -> it fails

Thursday, March 13, 2014 1:24 PM
• Have you tried this: devenv /build test Mysolution.sln

It builds the given solution using the given configuration via VS.  If that doesn't work because your VS instance has plugins causing issues or something then use MSBuild directly.

msbuild.exe mysolution.sln /t:Build/p:Configuration=test;Platform=AnyCPU

Clarification:  I'm still of the mindset that you should have a custom solution configuration that builds only the projects you want in your automated script.  This allows you to add and remove projects without touching your build script.  That is the assumption in the above code.  I'd also recommend that you discuss changing your dev process because in every place I've ever worked no one should check in code that doesn't build.  There are better ways to handle work in progress changes without committing it to TFS.

Michael Taylor
http://msmvps.com/blogs/p3net

Thursday, March 13, 2014 2:41 PM
• It doesn't build even with msbuild.exe

seems it doesn't give a crap about any build configuration. nice.

Thursday, March 13, 2014 2:54 PM
• Generate a build log so you can diagnose why it is failing.  That will identify where the build is failing.

Michael Taylor
http://msmvps.com/blogs/p3net

Thursday, March 13, 2014 4:41 PM
• i perfectly know why the solution doesn't build.

the solution doesn't build because there are three under-development projects.

the solution build if i manually unload/remove/exclude from build configuration those projects.

i want those projects not to be build by msbuild (and no, /p:Configuration switch clearly doesn't work).

do you even read my posts?

Thursday, March 13, 2014 4:49 PM
• MSbuild can build a single configuration/platform.  If it couldn't then TFS and VS builds would fail.   I suspect your command line isn't correct.

Here's a valid command line taken from one of my builds.  Substitution your solution file, configuration name and platform name accordingly.  When run from the command line you should get errors if the configuration or platform do not exist.  If they do exist and the build fails then you should be able to look at the log and verify that the projects were properly excluded.  If they weren't then your configuration isn't correct.

msbuild <solution file> /t:Build /p:Configuration=MyConfiguration;Platform="Any CPU"

Thursday, March 13, 2014 5:04 PM
• Yes, agreed this is solved by using MSBUILD but in particular creating a build definition whereby you pick what to build.  http://msdn.microsoft.com/en-us/library/ms181286(v=vs.90).aspx

Or did I not read this right?

JP Cowboy Coders Unite!

Thursday, March 13, 2014 8:12 PM
• ok, look.

my english is not fluent, i'll try to explain with some screenshots.

here is the default configuration, which include ALL projects in solution -> http://i.imgur.com/5mZkVGB.png

here is the msbuild output -> http://i.imgur.com/B1qm44e.png

here is the vs build output -> http://i.imgur.com/rigo74a.png

and my build script fails.

------------------------------------------

here is the test configuration WITHOUT under-development projects -> http://i.imgur.com/5JCKsjk.png

here is the msbuild output -> http://i.imgur.com/zjCnFg4.png

here is the vs build output -> http://i.imgur.com/xZGknCp.png

and my build script fails.

------------------------------------------

here is the msbuild result AFTER removing those projects -> http://i.imgur.com/FMPCdBj.png

here is the correspondant vs build result -> http://i.imgur.com/D6MdXJA.png

and my build script succeeds

in my humble opinion, these results are not coherent at all.

------------------------------------------

edit: before you ask, i use this msbuild command:

msbuild.exe mysolution.sln /t:Build /p:Configuration=Debug;Platform="Any CPU"

• Edited by Friday, March 14, 2014 9:53 AM
Friday, March 14, 2014 9:41 AM
• You're using the wrong configuration in the MSBuild command line.  You are specifying Debug as the configuration but you're solution configuration (based upon your screenshot) is called testConfiguration.  Adjust your build command line and try again.

msbuild.exe mysolution.sln /t:Build /p:Configuration=testConfiguration;Platform="Any CPU"

Friday, March 14, 2014 2:27 PM
• are you serious?

i thought it was obvious that i used /p:Configuration=Debug with "Debug" configuration and /p:Configuration=testConfiguration with "testConfiguration" configuration.

clearly i was wrong.

• Edited by Friday, March 14, 2014 2:34 PM
Friday, March 14, 2014 2:32 PM
• please DO NOT mark as answer useless posts just to assign some points.

also, i get an "unexpected error" when i try to unmark them.

nice work microsoft.

Friday, March 21, 2014 8:31 AM
• Hi doc90,