Backslash and quote in command-line arguments

前端 未结 5 771
攒了一身酷
攒了一身酷 2020-11-29 06:53

Is the following behaviour some feature or a bug in C# .NET?

Test application:

using System;
using System.Linq;

namespace ConsoleApplication1
{
             


        
5条回答
  •  清酒与你
    2020-11-29 07:06

    After much experimentation this worked for me. I'm trying to create a command to send to the Windows command line. A folder name comes after the -graphical option in the command, and since it may have spaces in it, it has to be wrapped in double quotes. When I used back slashes to create the quotes they came out as literals in the command. So this. . . .

    string q = @"" + (char) 34;
    string strCmdText = string.Format(@"/C cleartool update -graphical {1}{0}{1}", this.txtViewFolder.Text, q);
    System.Diagnostics.Process.Start("CMD.exe", strCmdText);
    

    q is a string holding just a double quote character. It's preceded with @ to make it a verbatim string literal.

    The command template is also a verbatim string literal, and the string.Format method is used to compile everything into strCmdText.

提交回复
热议问题