public static string EscapeArguments(params string[] args)
{
StringBuilder arguments = new StringBuilder();
Regex invalidChar = new Regex("[\x00\x0a\x0d]"); // these can not be escaped
Regex needsQuotes = new Regex(@"\s|"""); // contains whitespace or two quote characters
Regex escapeQuote = new Regex(@"(\\*)(""|$)"); // one or more '\' followed with a quote or end of string
foreach(var arg in args)
{
if (arg == null)
throw new ArgumentNullException("arg");
if (invalidChar.IsMatch(arg))
throw new ArgumentOutOfRangeException("arg");
if (arg == String.Empty)
{
arguments.Append("\"\"");
}
else if (!needsQuotes.IsMatch(arg))
{
arguments.Append(arg);
}
else
{
arguments.Append('"');
arguments.Append(escapeQuote.Replace(arg,
m =>
m.Groups[1].Value + m.Groups[1].Value + (m.Groups[2].Value == "\"" ? "\\\"" : "")));
arguments.Append('"');
}
arguments.Append(' ');
}
return arguments.ToString();
}