Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#
Article

BatchRebuilder - Batch rebuilder for your Visual Studio .NET Projects

Rate me:
Please Sign up or sign in to vote.
4.93/5 (18 votes)
23 Jan 20054 min read 65.7K   2K   51   7
This is a tool created to compile all C# or VB.NET projects in a specified folder. It'll recurse into the subdirectories looking for projects to compile. It has a lot of options like those for changing the project's target folder and logging. Hope you find it handy!

Sample Image - batchrebuilder.jpg

Introduction

A while ago, I was developing a project with a large number of components. The problem was that each time we need to compile the entire project, we need to download each individual component project from our source control tool, change the projects to release build configuration and compile them. It's an easy task, but it can become very time consuming if you have to compile about 10 components a day. The other problem I faced was that each project has its own target folder configuration, and I wanted to compile all the projects to the same target folder. To solve this problem, I created this handy little tool. It'll search all C# or VB.NET projects in a specified folder and rebuild them. There are some options available in the tool, that are described below.

The Project option

In the Project option, you'll be able to select the project type you want to search/rebuild. You can select C# or VB.NET projects. You'll also need to select the path where your project files are located (E.g.: \My Documents\Visual Studio Projects). The tool will recurse in to the subfolders of the specified path, looking for .csproj or .vbproj files. There's another option here, the Version option, which allows you to select the kind of compilation you want to use: Release or Debug.

The Destination options

In this section, you can configure the destination folder of your assemblies. If you check the first check box, the tool will update the configuration for the target folder of your project files with the folder specified (this change will be permanent, since we need to save the project file in order to compile it). You can also check the "Copy Content Option" checkbox, which is a very handy option for ASP.NET projects. This option will copy all the content files found in the project's folder to the specified folder (there is a fixed list of extensions, of files considered "content files").

Logging Options

In this section, you can specify the path to save the log files. Batch Rebuilder creates a log for each project it compiles, using the format: myprojectname_build.txt. By default, it will only save log files for projects that didn't compile successfully. You can change this option by unchecking the check box.

How does it work?

The project is very simple, it'll recurse in the subfolders of the specified project folder looking for .vbproj or .csproj files using the GetFiles function of the System.IO.Directory class. When it finds one, it will compile it using devenv.exe.

The devenv call is invoked using the Process class that is available in the System.Diagnostics namespace. The first thing we need to do is create a new instance of this class.

C#
Process oProcess = new Process();

If the user checked the option of copying the assemblies to a different folder, we'll open the project file and change the corresponding compilation target folder to the desired folder. Since the Visual Studio project files are XML documents, we'll use the XmlDocument class to update the project file with the new path.

C#
XmlDocument oPrjFile = new XmlDocument(); oPrjFile.Load(sFile);
XmlElement  oNode = (XmlElement) 
   oPrjFile.SelectSingleNode("/VisualStudioProject/" + 
   "VisualBasic/Build/Settings/Config[@Name='" 
   + cboVersion.SelectedItem.ToString() + "']");
    
if(chkCopyAssemblys.Checked) 
{ 
    oNode.Attributes["OutputPath"].Value = txtAssemblysTargetPath.Text; 
} 

oPrjFile.Save(sFile);

Now that the project file is set, we'll set the Process class properties to execute devenv.exe and compile the project. The first thing to do is to specify where devenv is. This is accomplished by reading a registry entry that indicates where Visual Studio is installed.

Next, we specify the arguments that need to be informed to devenv. The /out argument specifies the log file that will be created, and the /rebuild option indicates which version we want to rebuild (Release or Debug).

C#
oProcess.StartInfo.FileName = sDevEnvPath;

if(cboVersion.SelectedIndex == 0)
    oProcess.StartInfo.Arguments = "/rebuild RELEASE "; 
else 
    oProcess.StartInfo.Arguments = "/rebuild DEBUG "; 
    
oProcess.StartInfo.Arguments += " /out \"" + txtLogPath.Text +
 sProjectName + "_build.txt\" \"" + sFile + "\""; 
oProcess.StartInfo.RedirectStandardOutput =    true; 
oProcess.StartInfo.UseShellExecute = false; 
oProcess.Start();

After the compilation finishes, we test the exit code of devenv. If anything goes wrong with the compilation, the return code will be different than zero. In this case, we'll maintain the log file for further analysis.

C#
if(oProcess.ExitCode != 0) 
    txtOutput.AppendText("** Error compiling project!! ** " + "\n");
else { 
    if(chkLogOnlyOnFail.Checked) 
        System.IO.File.Delete(txtLogPath.Text + sProjectName + "_build.txt");
        string sProjectPath = Path.GetDirectoryName(sFile); 
    }

Final words

This tool was created to solve a specific problem (my problem, in this case), and I know that its functionality is very limited. I think that this tool can do a lot more, so... I'm up to suggestions. If anyone has any suggestions, just drop me a note!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Brazil Brazil
Mauricio Ritter lives in Brazil, in the city of Porto Alegre. He is working with software development for about 8 years, and most of his work was done at a bank, within a home and office banking system.
Mauricio also holds MCSD, MCSE, MCDBA, MCAD and MCT Microsoft certifications and work as a trainer/consultant in some MS CTEC in his city.
Mauricio also works in his own programming site, aimed to Brazilian Developers: http://www.dotnetmaniacs.com.br

In his spare time he studys korean language...

Comments and Discussions

 
QuestionHelp me! Pin
Member 46414186-Aug-08 4:44
Member 46414186-Aug-08 4:44 
Generalsource control and c# projects Pin
drornahmias29-Sep-05 3:57
drornahmias29-Sep-05 3:57 
Generalsuggestion Pin
Lakshminarayanan16-May-05 1:31
Lakshminarayanan16-May-05 1:31 
GeneralGreat Tool Pin
Peter C. Gallati28-Jan-05 7:11
Peter C. Gallati28-Jan-05 7:11 
GeneralNice Tool Pin
Peter C. Gallati28-Jan-05 7:09
Peter C. Gallati28-Jan-05 7:09 
GeneralRe: Nice Tool Pin
Christopher Scholten13-Jul-05 18:49
professionalChristopher Scholten13-Jul-05 18:49 
QuestionNice - but why bother? Pin
Marc Scheuner24-Jan-05 0:03
professionalMarc Scheuner24-Jan-05 0:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.