C#中的转义命令行参数

简洁版本:

将参数用引号引起来并转义\和是否足够"?


代码版本

我想string[] args使用ProcessInfo.Arguments 将命令行参数传递给另一个进程。


ProcessStartInfo info = new ProcessStartInfo();

info.FileName = Application.ExecutablePath;

info.UseShellExecute = true;

info.Verb = "runas"; // Provides Run as Administrator

info.Arguments = EscapeCommandLineArguments(args);

Process.Start(info);

问题是我将参数作为数组获取,必须将它们合并为单个字符串。可以设计一个参数来欺骗我的程序。


my.exe "C:\Documents and Settings\MyPath \" --kill-all-humans \" except fry"

根据这个答案,我创建了以下函数来转义单个参数,但是我可能错过了一些东西。


private static string EscapeCommandLineArguments(string[] args)

{

    string arguments = "";

    foreach (string arg in args)

    {

        arguments += " \"" +

            arg.Replace ("\\", "\\\\").Replace("\"", "\\\"") +

            "\"";

    }

    return arguments;

}

这足够好还是有任何框架功能呢?


慕尼黑8549860
浏览 253回答 3
3回答

哈士奇WWW

我也遇到这个问题。我没有解析args,而是采用了完整的原始命令行并修剪了可执行文件。即使不需要/未使用,这也具有在呼叫中保留空格的额外好处。它仍然必须在可执行文件中追逐转义符,但这似乎比args容易。var commandLine = Environment.CommandLine;var argumentsString = "";if(args.Length > 0){&nbsp; &nbsp; // Re-escaping args to be the exact same as they were passed is hard and misses whitespace.&nbsp; &nbsp; // Use the original command line and trim off the executable to get the args.&nbsp; &nbsp; var argIndex = -1;&nbsp; &nbsp; if(commandLine[0] == '"')&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //Double-quotes mean we need to dig to find the closing double-quote.&nbsp; &nbsp; &nbsp; &nbsp; var backslashPending = false;&nbsp; &nbsp; &nbsp; &nbsp; var secondDoublequoteIndex = -1;&nbsp; &nbsp; &nbsp; &nbsp; for(var i = 1; i < commandLine.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(backslashPending)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; backslashPending = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(commandLine[i] == '\\')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; backslashPending = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(commandLine[i] == '"')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; secondDoublequoteIndex = i + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; argIndex = secondDoublequoteIndex;&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // No double-quotes, so args begin after first whitespace.&nbsp; &nbsp; &nbsp; &nbsp; argIndex = commandLine.IndexOf(" ", System.StringComparison.Ordinal);&nbsp; &nbsp; }&nbsp; &nbsp; if(argIndex != -1)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; argumentsString = commandLine.Substring(argIndex + 1);&nbsp; &nbsp; }}Console.WriteLine("argumentsString: " + argumentsString);
打开App,查看更多内容
随时随地看视频慕课网APP