简体   繁体   中英

How can I programmatically build my solution files from C# code?

I have a large solution containing many projects with one of them being a Setup project. There are also many current versions stored in separate branches. I have a build tool that used to work in .NET 2, but hasn't worked since we upgraded to .NET 4.

Internally, the new .NET 4 version of the build tool uses Microsoft.TeamFoundation.Client.RegisteredTfsConnections.GetProjectCollections() and versionControlServer.GetAllTeamProjects(false) to get a collection of TeamProject s from my TFS source control server.

I then display them visually in the UI and when a user clicks on a particular solution version, the application calls the following to get the latest for that solution version:

workspace.Get(new string[] { serverPath }, VersionSpec.Latest, RecursionType.Full, 
   GetOptions.GetAll);

The application used to build the solution files and this would include the Setup project. At this stage, the setup project would create an MSI that the application could be installed with. It is this last step that I am having problems with.

I need to be able to programmatically build the solution that the user selected using C# code. The working .NET 2 code for this was as follows:

Process process = new Process();
ProcessStartInfo processStartInfo = process.StartInfo;
processStartInfo.FileName = processName;
processStartInfo.Arguments = string.Format(" \"{0}\" /BUILD \"Release|Any CPU\"", 
   solutionPath);
processStartInfo.WorkingDirectory = processDirectory;
process.Start();

There is no error when this is run, but it no longer launches Visual Studio and builds the code. Clearly, this was a poor way to do it initially, but I can't find the 'correct' way using the TFS classes.

I also tried running MSBuild.exe directly (similar to the above example), and this does build the solution, but for some reason does not build the Setup project that produces the MSI. Note that I do NOT use any manually created build files.

Unfortunately, useful documentation for the Microsoft.TeamFoundation namespace is hard to find! I'm hoping that someone here has made use of these classes and can direct me to a solution to this problem.

If at all possible, I need to use .NET classes (eg. not Process.Start) as I really need to know when the build has finished as well. I can however set up a FileSystemWatcher object for this if this is asking too much.

msbuild skips the installer projects because it doesn't know how to build them. FinalBuilder shells out to devenv.exe to build these.

Calling `devenv /build "Release|Any CPU" /project "MyInstaller.vdproj" should run the build you need from the command line, without starting the VS GUI. Try it!

With all that said: We, too, use FinalBuilder, and VS Installer is deprecated, so you'll probably want to plan on replacing that.

It turns out that the problem was unfortunately completely unrelated. The .NET 2 code that was to be updated had hard coded 'Program Files' into the devenv.exe file path - however the new computers are 64bit and Visual Studio 2010 is installed into 'Program Files (x86)'!

So this means that the above code sample DOES work and WILL allow me to build the solutions from C#. It's not the object oriented way that I would have preferred to do it, but after all your comments, I'm just glad to have got it working at all.

Thanks for your time everyone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM