Click here to Skip to main content
13,247,891 members (46,869 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 19 Jun 2006

Building and Deploying C# and VB projects without Visual Studio .NET: class and Command Line Utility ProjectBuilder.

, 19 Jun 2006
Rate this:
Please Sign up or sign in to vote.
This class and command line utility shows how to easily build and deploy C# and VB projects without VS.NET.
Sample Image - ProjectBuilder.jpg

Introduction: What Is It All About?

This article describes a C# class and command line utility based on it, which helps to automatically build and deploy .NET applications written in C# or VB.NET without Visual Studio .NET 2003 only using .NET Framework. The idea of writing it came to me when I had trouble with adding new functionality to my application when no Visual Studio .NET was available. I needed a way to quickly change code and rebuild the project. Another reason to do it was the necessity of having a chance to deploy a Web application from a development environment to a test environment and after that to production in an automatic manner. There are ways to do automatic deployment with your hand-written code in *.bat files already. But unfortunately BAT files are sometimes an awkward way to do the task. When you gather files from your project folder with BAT, you have to use wildcards ( *.cs, *.cpp) and it is not a simple task to check which ones are included in the project and which aren't (you should use project file and XPath to detect files, which should be built). With this class, you can deploy only content files and DLL automatically to the folder you set as your test server's application folder. You could also create a scheduled task for the utility in your OS to compile and put your software to test server even if you are not there at the computer at the desired time.

Class Builder

Here is the interface of the Builder class:

///Build in debug mode
bool Debug { get; set; }

///The name of a file on disk
string ProjectFileName { get; }

///Path to the project file
string ProjectFilePath { get;  set; }

///Other parameters, which are sent to CSC compiler directly
string OtherCscParams { get; set; }

///Assembly type: Exe, WinExe, Library, Module
AssemblyType BuildAssemblyType { get; }

string BuildAssemblyName { get; }
Language AssemblyLanguage { get; }
string MainClass { get; }
string RootNamespace { get; }

public readonly ArrayList Files;
public readonly ArrayList Resources;
public readonly ArrayList References;

void CopyContent(string pOutputPath);
void CopyContent(string pOutputPath, bool pCopyAssemblyFromBin);
void Build(string pOutputPath);

Working with Builder Class

Using the Builder class is easy. First of all, add a reference to prjbuilder.dll (go to "Add References..." -> Browse...-> Select prjbuilder.dll). Then add using prjbuilder; to the code file. I'll show an example of using ProjectBuilder. Sorry for the fact that I omitted some code, but I made it only for the sake of readability of the code here.

Builder b = null;
    log.putLine("Reading project file " + prjFile + ".");
    b = new Builder(debug, prjFile, others);
    log.putLine("Project file read.");
catch(Exception ex)

if(b != null)
        log.putLine("Build started.");

        // Actually building the project

        log.putLine("Build finished.");

    if(outputPath != "")
        log.putLine("Copying started.");

        // Copying all content files, except for old assembly DLL
        b.CopyContent(outputPath, !dobuild);

        log.putLine("Copying finished.");

Class Usage Points

The Builder class can build and deploy projects. So, with the Builder class, you could make your own builder applications which will build and deploy code without Visual Studio .NET. There are a lot of ways in which you can use it:

  1. You could use the class in your local development process to compile code which has changed a little, without the need to wait until heavy Visual Studio will start up.
  2. Make some automatic utility which will build your applications when you are not beside your computer by a schedule and then put it to the test environment automatically.
  3. Write a Web Service or Web application which uses ProjectBuilder to build and deploy your app remotely on the target machine. Note that the existence of Visual Studio .NET on the target machine is not required.

Inside the Build Method

Build is done by framework classes from the namespace System.CodeDom.Compiler.*. First we use XPath to know which files of each group project include: compile, resource and referenced files. After that we fill the corresponding ArrayLists: Files, Resources, References. After all, we form the parameters for compiler, passing files to build, resource files and referenced files. Here is the Builder.Build(string pOutputPath) method.

public void Build(string pOutputPath)
    // Initialize properties from the project file

    // Changing current directory to project file's folder
    string oldFolder = Environment.CurrentDirectory;
    string sDir = ProjectFilePath.Replace(ProjectFileName, "");
    if (sDir != "")
        Environment.CurrentDirectory    = sDir;

    // Getting Files, Resources, References from the project file

    // Getting Imports, this should be done only for VB
    if(AssemblyLanguage == Language.VB)

    string buildAssmTypeAttr    = "/t:library";
        case AssemblyType.Library:
        case AssemblyType.Module:
            buildAssmTypeAttr = "/t:module";
        case AssemblyType.Exe:
            buildAssmTypeAttr = "/t:exe";
        case AssemblyType.WinExe:
            buildAssmTypeAttr = "/t:winexe";

    string compilerName = "csc.exe";
        case Language.CSharp:
        case Language.VB:
            compilerName = "vbc.exe";

    string outDir = pathToBin(pOutputPath);

    //Create an instance whichever code provider that is needed
    CodeDomProvider codeProvider = null;
        case Language.CSharp:
            codeProvider = new CSharpCodeProvider();
        case Language.VB:
            codeProvider = new VBCodeProvider();

    //create the language specific code compiler
    ICodeCompiler compiler = codeProvider.CreateCompiler();

    //add compiler parameters
    CompilerParameters compilerParams = new CompilerParameters();
    compilerParams.CompilerOptions  = buildAssmTypeAttr + " " +
        (AssemblyLanguage == Language.VB? "/rootnamespace:" + RootNamespace: "")
        + OtherCscParams.Trim();
    compilerParams.OutputAssembly = outDir + BuildAssemblyName +
        "." + buildAssemblyExt;
    compilerParams.GenerateExecutable = BuildAssemblyType ==
        AssemblyType.Exe || BuildAssemblyType == AssemblyType.WinExe;
    compilerParams.GenerateInMemory = false;
    compilerParams.IncludeDebugInformation = Debug;
    compilerParams.MainClass = MainClass;

    // Adding all References
    foreach(string rel in References)

    // Adding all Resources
    foreach(string res in Resources)
        compilerParams.CompilerOptions += " /res:\"" + res + "\"";

    if(AssemblyLanguage == Language.VB)
    // Adding all imports
    foreach(string import in Imports)
        compilerParams.CompilerOptions += " /imports:" + import;

    bool buildSuccessful = false;
        if(log != null)
            log.putLine("Using compiler: " + compilerName + "\n");
            log.putParameters(Files, Resources, References, Imports);

        string[] compileFiles = new string[Files.Count];

        //actually compile the code
        CompilerResults results = compiler.CompileAssemblyFromFileBatch(

        string sOutput = "";
        foreach(string s in results.Output)
            sOutput += "\n" + s;

        if(log != null)

        // Copying all content files, except for old assembly DLL
        copyFiles(localLibs, pathToBin(pOutputPath), false);

        if(log != null)

        buildSuccessful = true;
    catch(Exception ex)
        if(log != null)
        log.put("Error: " + ex.Message + "\n" +
        "Stack trace: \n" + ex.StackTrace);
        Environment.CurrentDirectory = oldFolder;

    return buildSuccessful;

Utility Usage

The utility uses ProjectBuilder.Builder class to build and deploy .NET C# and VB projects. You could also easily extract only the deployment part from your project, which consists of:

  1. Assembly DLL
  2. Referenced DLLs copied to the bin folder
  3. Content files:

    prjbuilder.exe [/release] [/copy:destination] [/nobuild] 
        [other parameters for framework compiler] project_name
    • /release: Forces compiler to do release build
    • /copy:destination: Sets the destination directory, where only content and compiled assemblies will be placed saving the catalog structure.
      1. Copied files replace old ones in the destination folder.
      2. If path consists of white spaces, then include it in double quotes "", do not use trailing slash. Example:
        • prjbuilder.exe /copy:"C\Program files\my app"
    • /nobuild: When this option is used, no build is done. It is useful when you need to copy the project to some destination without compiling.
    • project_name: Specifies path to the project to build.
    • Other parameters for framework compiler: These parameters are passed to the .NET compiler without changes. It was done to help to work in situations when you want to pass a parameter directly to the compiler.


The compiler makes build logs and places them into the project file's folder. If the project could not be found, then logs are placed into the current directory - build.log. This file consists of all the information, which is printed when the build is done - build_files.log. All files, which take part in the build process: compiled files, resource files, reference files.


  1. Simply building the project in debug mode:

  2. prjbuilder.exe c:\dev\test\test.csproj 
    </li />
  3. Building project in release mode:

    prjbuilder.exe /release "C:\Documents and Settings\devuser\My Documents\
        Visual Studio Projects\test\test.vbproj" 
  4. Building and copying project (note, that in the source project's folder, DLLs are not changed):

    prjbuilder.exe /copy:"C:\folder with spaces" c:\dev\test\test.csproj 
  5. Make a deployable copy of the project without building it (note that DLL, content and referenced assemblies are copied only):

    prjbuilder.exe /copy:"C:\folder with spaces" /nobuild c:\dev\test\test.csproj 


  • 20th June, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mihail Arhipov
Russian Federation Russian Federation
My name is Mihail Arhipov. I live in Saint-Petersburg, Russia. I am studing at university 4th course now and working as a developer in C#, ASP.NET, Oracle, MS SQL for 1,5 year already.

You may also be interested in...

Comments and Discussions

Questionthe Xpath is unable to find the xml tags Pin
anilkumar1437-Dec-15 21:21
memberanilkumar1437-Dec-15 21:21 
QuestionProblems with libs Pin
titocozza11-Nov-06 10:40
membertitocozza11-Nov-06 10:40 
AnswerRe: Problems with libs Pin
Mihail Arhipov13-Nov-06 0:15
memberMihail Arhipov13-Nov-06 0:15 
QuestionRe: Problems with libs Pin
titocozza13-Nov-06 11:16
membertitocozza13-Nov-06 11:16 
Generalhi Pin
pkarthik197924-Jul-06 23:16
memberpkarthik197924-Jul-06 23:16 
AnswerRe: hi Pin
Mihail Arhipov26-Jul-06 0:49
memberMihail Arhipov26-Jul-06 0:49 
GeneralCommandline work Pin
intrader26-Jun-06 13:06
memberintrader26-Jun-06 13:06 
GeneralGood job! Some requests... Pin
HyperX20-Jun-06 5:30
memberHyperX20-Jun-06 5:30 
GeneralRe: Good job! Some requests... Pin
Mihail Arhipov20-Jun-06 21:13
memberMihail Arhipov20-Jun-06 21:13 
GeneralRe: Good job! Some requests... Pin
tsvayer27-Jun-06 4:22
membertsvayer27-Jun-06 4:22 
GeneralRe: Good job! Some requests... Pin
Software_Architect27-Jun-06 10:23
memberSoftware_Architect27-Jun-06 10:23 
GeneralRe: Good job! Some requests... Pin
Vitaliy Tsvayer27-Jun-06 11:22
memberVitaliy Tsvayer27-Jun-06 11:22 
Generalmsbuild.exe Pin
Igor Vigdorchik20-Jun-06 4:16
memberIgor Vigdorchik20-Jun-06 4:16 
GeneralRe: msbuild.exe Pin
Mihail Arhipov20-Jun-06 20:59
memberMihail Arhipov20-Jun-06 20:59 
GeneralRe: msbuild.exe Pin
Igor Vigdorchik21-Jun-06 4:13
memberIgor Vigdorchik21-Jun-06 4:13 
GeneralRe: msbuild.exe Pin
Mihail Arhipov21-Jun-06 20:28
memberMihail Arhipov21-Jun-06 20:28 
GeneralRe: msbuild.exe Pin
Igor Vigdorchik22-Jun-06 4:40
memberIgor Vigdorchik22-Jun-06 4:40 
GeneralRe: msbuild.exe Pin
hauer26-Jun-06 7:04
memberhauer26-Jun-06 7:04 
GeneralRe: msbuild.exe Pin
BeSoft6-Nov-06 23:03
memberBeSoft6-Nov-06 23: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 20 Jun 2006
Article Copyright 2006 by Mihail Arhipov
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid