Export (0) Print
Expand All

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.

  NameDescription
Public methodCommandLineBuilder()Initializes a new instance of the CommandLineBuilder class.
Public methodCommandLineBuilder(Boolean)Default constructor
Top

  NameDescription
Protected propertyCommandLineGets the StringBuilder instance representing the command line for inheriting classes.
Public propertyLengthRepresents the length of the command line.
Top

  NameDescription
Public methodAppendFileNameIfNotNull(ITaskItem)Appends the command line with the file name of the specified ITaskItem object.
Public methodAppendFileNameIfNotNull(String)Appends the command line with file name represented by the parameter, inserting quotation marks if necessary.
Public methodAppendFileNamesIfNotNull(ITaskItem[], String)Appends the command line with the list of file names in the specified ITaskItem array, separated by the specified delimiter.
Public methodAppendFileNamesIfNotNull(String[], String)Appends the command line with the list of file names in the specified string array, separated by the specified delimiter.
Protected methodAppendFileNameWithQuotingAppends the command line with a file name, and surrounds the file name with quotation marks as necessary.
Protected methodAppendQuotedTextToBufferAppends given text to the buffer after first quoting the text if necessary.
Protected methodAppendSpaceIfNotEmptyAdds a space to the specified string, given the string is not empty.
Public methodAppendSwitchAppends the command line with the specified switch.
Public methodAppendSwitchIfNotNull(String, ITaskItem)Appends the command line with a switch that takes a task item specification that acts a single string parameter.
Public methodAppendSwitchIfNotNull(String, String)Appends the command line with a switch that takes a single string parameter.
Public methodAppendSwitchIfNotNull(String, ITaskItem[], String)Appends the command line with a switch that takes an array of task item specifications that act as string parameters.
Public methodAppendSwitchIfNotNull(String, String[], String)Appends the command line with a switch that takes an array of string parameters.
Public methodAppendSwitchUnquotedIfNotNull(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 methodAppendSwitchUnquotedIfNotNull(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 methodAppendSwitchUnquotedIfNotNull(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 methodAppendSwitchUnquotedIfNotNull(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 methodAppendTextUnquotedAppends the command line with string, without attempting to encapsulate the string with quotation marks.
Protected methodAppendTextWithQuotingAppends the command line with string, and surrounds the string with quotations marks as necessary.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodIsQuotingRequiredDetermines whether the specified string parameter should be surrounded with quotation marks because it contains white space.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns the command line as a string. (Overrides Object.ToString().)
Protected methodVerifyThrowNoEmbeddedDoubleQuotesReturns 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.

Community Additions

ADD
Show:
© 2014 Microsoft