private static string JoinArguments(IEnumerable<string> arguments)
{
if (arguments == null)
throw new ArgumentNullException("arguments");
StringBuilder builder = new StringBuilder();
foreach (string argument in arguments)
{
if (builder.Length > 0)
builder.Append(' ');
if (argument.IndexOfAny(new[] { '"', ' ' }) < 0)
{
builder.Append(argument);
continue;
}
// escape a backslash appearing before a quote
string arg = argument.Replace("\\\"", "\\\\\"");
// escape double quotes
arg = arg.Replace("\"", "\\\"");
// wrap the argument in outer quotes
builder.Append('"').Append(arg).Append('"');
}
return builder.ToString();
}