How to: Compile at the Command Prompt

There are several ways to compile a .NET Compact Framework application at the command prompt. These include the following:

  • Using the Microsoft Build Engine (MSBuild).

  • Using batch files.

Both methods use the Visual C# and Visual Basic compilers that are provided by the Windows Software Development Kit (SDK). However, MSBuild requires .targets files that are currently shipped only in Visual Studio.

Compiling by Using MSBuild

To build from the command prompt by using MSBuild

  1. Ensure that MSBuild.exe is in your system path.

    MSBuild.exe is located in %systemroot%\Microsoft.NET\Framework\version. The system path variable must include this path.

  2. At a command prompt, navigate to the directory where the project file for the .NET Compact Framework application is located. The project file will have the extension .sln, .vbproj, or .csproj.

  3. Type the following command.

    msbuild.exe projectname
    

    The Command Prompt window provides information about the build process.

Compiling by Using Batch Files

You can use batch files to target the .NET Compact Framework assemblies instead of the full .NET Framework (desktop) assemblies. Batch files can be used directly or can be invoked from a make file utility to enable the integration of the .NET Compact Framework into enterprise build systems.

The batch file includes the following information:

  • A path variable to the location of the .NET Compact Framework assemblies. The following example uses NETCF_PATH.

    Note

    Alternatively, you can specify this path in a make file or as an environment variable by using the My ComputerProperties dialog box.

  • References to assemblies, as required. You use the -r switch and concatenate the assemblies into a variable.

  • Compiler commands with the appropriate switches and arguments, as described in the "Compiler Switches" section later in this topic. The arguments include variable references and an asterisk (*) to incorporate additional arguments.

To create a batch file for compilation

  1. Create a new file in a text editor and add the contents of the sample batch file, as shown in the following example, for your preferred programming language.

  2. Adjust the NETCF_PATH variable, if necessary.

    By default, the .NET Compact Framework assemblies are located in the C:\Program Files\Microsoft.NET\SDK\version\CompactFramework\WindowsCE folder.

    Note

    If you are using the .NET Compact Framework included with Visual Studio 2008 the assemblies are in C:\Program Files\Visual Studio version\SmartDevices\SDK\CompactFramework\version\WindowsCE.

  3. Save and name the file, such as vbcc.bat for a Visual Basic batch file or cscc.bat for a C# batch file.

To compile an application by using a batch file

  1. Ensure that your system path includes the .NET Framework.

    The Visual Basic compiler (vbc.exe) and Visual C# compiler (csc.exe) are located in the %systemroot%\Microsoft.NET\Framework\version folder. The system path variable must include this path.

  2. At a command prompt, enter the name of the batch file followed by the source file; for example:

    vbcc myapp.vb
    

    The source file must have a .cs or .vb extension, depending on which compiler will be used.

The executable file, which can be copied to the device or emulator, is built in the current directory.

Batch File for C# Compilation

The following is a sample batch file for C# compilation.

@echo off

if "%NETCF_PATH%" == "" ( set "NETCF_PATH=C:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE" )

if DEFINED REF ( set REF= )

set REF=%REF% "/r:%NETCF_PATH%\MsCorlib.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Core.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Data.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Drawing.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Messaging.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Net.IrDA.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Web.Services.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Windows.Forms.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.ServiceModel.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Runtime.Serialization.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Xml.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Xml.Linq.dll"
set REF=%REF% "/r:%NETCF_PATH%\Microsoft.WindowsCE.Forms.dll"
set REF=%REF% "/r:%NETCF_PATH%\Microsoft.WindowsMobile.DirectX.dll"
set REF=%REF% "/r:%NETCF_PATH%\Microsoft.ServiceModel.Channels.Mail.dll"
set REF=%REF% "/r:%NETCF_PATH%\Microsoft.ServiceModel.Channels.Mail.WindowsMobile.dll"

rem :compile

csc -nostdlib -noconfig %REF% %*

Batch File for Visual Basic Compilation

The following is a sample batch file for Visual Basic compilation.

@echo off

rem Verify the path is correct.
if "%NETCF_PATH%" == "" (
  set "NETCF_PATH=C:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE" )
set SDK_PATH="%NETCF_PATH%"

if DEFINED REF ( set REF= )

set REF=%REF% "/r:%NETCF_PATH%\System.Data.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Core.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Drawing.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Messaging.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Net.IrDA.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Web.Services.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Windows.Forms.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.ServiceModel.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Runtime.Serialization.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Xml.dll"
set REF=%REF% "/r:%NETCF_PATH%\System.Xml.Linq.dll"
set REF=%REF% "/r:%NETCF_PATH%\Microsoft.WindowsCE.Forms.dll"
set REF=%REF% "/r:%NETCF_PATH%\Microsoft.WindowsMobile.DirectX.dll"
set REF=%REF% "/r:%NETCF_PATH%\Microsoft.ServiceModel.Channels.Mail.dll"
set REF=%REF% "/r:%NETCF_PATH%\Microsoft.ServiceModel.Channels.Mail.WindowsMobile.dll"
set REF=%REF% "/r:%NETCF_PATH%\Microsoft.VisualBasic.dll"

set IMP= /Imports:Microsoft.VisualBasic,System,System.Data,System.Drawing,System.Windows.Forms

vbc -sdkpath:%SDK_PATH% -netcf -noconfig -nostdlib %IMP% %REF% %*

Compiler Switches

The following table describes the compiler switches and arguments.

Switch

Description

-r

References assemblies as required.

You can specify assemblies by setting an environment variable, such as REF. Most .NET Compact Framework assemblies are referenced by default.

C# requires an explicit reference to Mscorlib.dll, and Visual Basic uses the -sdkpath switch to locate it.

-imports

Visual Basic requires explicit imports for the following namespaces:

  • Microsoft.VisualBasic

  • System.System.Data

  • System.Drawing

  • System.Windows.Forms

-nostdlb

Instructs the Visual C# compiler to ignore the assemblies of the full (desktop) .NET Framework. For the Visual Basic compiler, this prevents the compiler from reading the Vbc.rsp file.

-netcf

Instructs the Visual Basic compiler to ignore the assemblies of the full (desktop) .NET Framework.

-noconfig

Required.

-sdkpath

Required for Visual Basic to locate Mscorlib.dll.

See Also

Other Resources

Building Applications and Core Tasks in the .NET Compact Framework

C# Compiler Options

Visual Basic Compiler

Change History

Date

History

Reason

July 2008

Included information on requirement to obtain .targets files by installing Visual Studio.

Content bug fix.

October 2008

Updated set command to help avoid issues with spaces in the path.

Content bug fix.