CommandLineBuilder Class

 

Comprises utility methods for constructing a command line.

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

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

public class CommandLineBuilder

NameDescription
System_CAPS_pubmethodCommandLineBuilder()

Initializes a new instance of the CommandLineBuilder class.

System_CAPS_pubmethodCommandLineBuilder(Boolean)

Default constructor

NameDescription
System_CAPS_protpropertyCommandLine

Gets the StringBuilder instance representing the command line for inheriting classes.

System_CAPS_pubpropertyLength

Represents the length of the command line.

NameDescription
System_CAPS_pubmethodAppendFileNameIfNotNull(ITaskItem)

Appends the command line with the file name of the specified ITaskItem object.

System_CAPS_pubmethodAppendFileNameIfNotNull(String)

Appends the command line with file name represented by the parameter, inserting quotation marks if necessary.

System_CAPS_pubmethodAppendFileNamesIfNotNull(ITaskItem[], String)

Appends the command line with the list of file names in the specified ITaskItem array, separated by the specified delimiter.

System_CAPS_pubmethodAppendFileNamesIfNotNull(String[], String)

Appends the command line with the list of file names in the specified string array, separated by the specified delimiter.

System_CAPS_protmethodAppendFileNameWithQuoting(String)

Appends the command line with a file name, and surrounds the file name with quotation marks as necessary.

System_CAPS_protmethodAppendQuotedTextToBuffer(StringBuilder, String)

Appends given text to the buffer after first quoting the text if necessary.

System_CAPS_protmethodAppendSpaceIfNotEmpty()

Adds a space to the specified string, given the string is not empty.

System_CAPS_pubmethodAppendSwitch(String)

Appends the command line with the specified switch.

System_CAPS_pubmethodAppendSwitchIfNotNull(String, ITaskItem)

Appends the command line with a switch that takes a task item specification that acts a single string parameter.

System_CAPS_pubmethodAppendSwitchIfNotNull(String, ITaskItem[], String)

Appends the command line with a switch that takes an array of task item specifications that act as string parameters.

System_CAPS_pubmethodAppendSwitchIfNotNull(String, String)

Appends the command line with a switch that takes a single string parameter.

System_CAPS_pubmethodAppendSwitchIfNotNull(String, String[], String)

Appends the command line with a switch that takes an array of string parameters.

System_CAPS_pubmethodAppendSwitchUnquotedIfNotNull(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.

System_CAPS_pubmethodAppendSwitchUnquotedIfNotNull(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.

System_CAPS_pubmethodAppendSwitchUnquotedIfNotNull(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.

System_CAPS_pubmethodAppendSwitchUnquotedIfNotNull(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.

System_CAPS_pubmethodAppendTextUnquoted(String)

Appends the command line with string, without attempting to encapsulate the string with quotation marks.

System_CAPS_protmethodAppendTextWithQuoting(String)

Appends the command line with string, and surrounds the string with quotations marks as necessary.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodIsQuotingRequired(String)

Determines whether the specified string parameter should be surrounded with quotation marks because it contains white space.

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns the command line as a string.(Overrides Object.ToString().)

System_CAPS_protmethodVerifyThrowNoEmbeddedDoubleQuotes(String, String)

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.

The following example creates a ToolTask that runs Ilasm.exe (IL 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
Available since 2.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: