This topic has not yet been rated - Rate this topic

CommandLineBuilder Class

Comprises utility methods for constructing a command line.

System.Object
  Microsoft.Build.Utilities.CommandLineBuilder
    Microsoft.Build.Tasks.CommandLineBuilderExtension

Namespace:  Microsoft.Build.Utilities
Assembly:  Microsoft.Build.Utilities.v4.0 (in Microsoft.Build.Utilities.v4.0.dll)
public class CommandLineBuilder

The CommandLineBuilder type exposes the following members.

  Name Description
Public method CommandLineBuilder() Initializes a new instance of the CommandLineBuilder class.
Public method CommandLineBuilder(Boolean) Default constructor
Top
  Name Description
Protected property CommandLine Gets the StringBuilder instance representing the command line for inheriting classes.
Public property Length Represents the length of the command line.
Top
  Name Description
Public method AppendFileNameIfNotNull(ITaskItem) Appends the command line with the file name of the specified ITaskItem object.
Public method AppendFileNameIfNotNull(String) Appends the command line with file name represented by the parameter, inserting quotation marks if necessary.
Public method AppendFileNamesIfNotNull(ITaskItem[], String) Appends the command line with the list of file names in the specified ITaskItem array, separated by the specified delimiter.
Public method AppendFileNamesIfNotNull(String[], String) Appends the command line with the list of file names in the specified string array, separated by the specified delimiter.
Protected method AppendFileNameWithQuoting Appends the command line with a file name, and surrounds the file name with quotation marks as necessary.
Protected method AppendQuotedTextToBuffer Appends given text to the buffer after first quoting the text if necessary.
Protected method AppendSpaceIfNotEmpty Adds a space to the specified string, given the string is not empty.
Public method AppendSwitch Appends the command line with the specified switch.
Public method AppendSwitchIfNotNull(String, ITaskItem) Appends the command line with a switch that takes a task item specification that acts a single string parameter.
Public method AppendSwitchIfNotNull(String, String) Appends the command line with a switch that takes a single string parameter.
Public method AppendSwitchIfNotNull(String, ITaskItem[], String) Appends the command line with a switch that takes an array of task item specifications that act as string parameters.
Public method AppendSwitchIfNotNull(String, String[], String) Appends the command line with a switch that takes an array of string parameters.
Public method AppendSwitchUnquotedIfNotNull(String, ITaskItem) Appends the command line with a switch that takes a task item specification as a single string parameter, without attempting to encapsulate the switch parameter with quotation marks.
Public method AppendSwitchUnquotedIfNotNull(String, String) Appends the command line with a switch that takes a single string parameter, without attempting to encapsulate the switch parameter with quotation marks.
Public method AppendSwitchUnquotedIfNotNull(String, ITaskItem[], String) Appends the command line with a switch that takes an array of task item specifications that act as string parameters, without attempting to encapsulate them with quotation marks.
Public method AppendSwitchUnquotedIfNotNull(String, String[], String) Appends the command line with a switch that takes an array of string parameters, without attempting to encapsulate switch parameters with quotation marks.
Public method AppendTextUnquoted Appends the command line with string, without attempting to encapsulate the string with quotation marks.
Protected method AppendTextWithQuoting Appends the command line with string, and surrounds the string with quotations marks as necessary.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method IsQuotingRequired Determines whether the specified string parameter should be surrounded with quotation marks because it contains white space.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns the command line as a string. (Overrides Object.ToString().)
Protected method VerifyThrowNoEmbeddedDoubleQuotes Returns an error if the command line parameter contains a double-quote (") character. Because double quotes are illegal in command line parameters, this method helps prevent parameter injection attacks.
Top

The following example creates a ToolTask that runs Ilasm.exe (MSIL Assembler). The CommandLineBuilder, ToolLocationHelper, and TaskLoggingHelper classes are used to generate the information needed to run the task.


using System;
using System.Collections;
using System.Text;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;

namespace MSBuildTasks
{
    /// <summary>
    /// A very simple and incomplete ToolTask to wrap the ILASM.EXE tool.
    /// </summary>
    public class ILAsm : ToolTask
    {
        #region Member Data
        /// <summary>
        /// Gets the collection of parameters used by the task class.
        /// </summary>
        /// <value>Parameter bag.</value>
        protected internal Hashtable Bag
        {
            get
            {
                return bag;
            }
        }

        private Hashtable bag = new Hashtable();
        #endregion

        #region ILAsm Task Properties
        /// <summary>
        /// The Source file that is to be compled (.il)
        /// </summary>
        public ITaskItem Source
        {
            get { return Bag["Source"] as ITaskItem; }
            set { Bag["Source"] = value; }
        }
        /// <summary>
        /// Either EXE or DLL indicating the assembly type to be generated
        /// </summary>
        public string TargetType
        {
            get { return Bag["TargetType"] as string; }
            set { Bag["TargetType"] = value; }
        }
        #endregion

        #region ToolTask Members
        protected override string ToolName
        {
            get { return "ILAsm.exe"; }
        }

        /// <summary>
        /// Use ToolLocationHelper to find ILASM.EXE in the Framework directory
        /// </summary>
        /// <returns></returns>
        protected override string GenerateFullPathToTool()
        {
            // Ask ToolLocationHelper to find ILASM.EXE - it will look in the latest framework directory available
            return ToolLocationHelper.GetPathToDotNetFrameworkFile(ToolName, TargetDotNetFrameworkVersion.VersionLatest);
        }
        #endregion

        #region ILAsm Task Members
        /// <summary>
        /// Construct the command line from the task properties by using the CommandLineBuilder
        /// </summary>
        /// <returns></returns>
        protected override string GenerateCommandLineCommands()
        {
            CommandLineBuilder builder = new CommandLineBuilder();

            // We don't need the tool's logo information shown
            builder.AppendSwitch("/nologo");

            string targetType = Bag["TargetType"] as string;
            // Be explicit with our switches
            if (targetType != null)
            {
                if (String.Compare(targetType, "DLL", true) == 0)
                {
                    builder.AppendSwitch("/DLL");
                }
                else if (String.Compare(targetType, "EXE", true) == 0)
                {
                    builder.AppendSwitch("/EXE");
                }
                else
                {
                    Log.LogWarning("Invalid TargetType (valid values are DLL and EXE) specified: {0}", targetType);
                }
            }

            // Add the filename that we want the tool to process
            builder.AppendFileNameIfNotNull(Bag["Source"] as ITaskItem);

            // Log a High importance message stating the file that we are assembling
            Log.LogMessage(MessageImportance.High, "Assembling {0}", Bag["Source"]);

            // We have all of our switches added, return the commandline as a string
            return builder.ToString();
        }
        #endregion
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ