问题描述
我想在 C# 中执行一个 cmd 命令。以下命令 AAA 在 Windows 10 cmd 中工作。以下代码的字符串 AAA 部分在 C# 中的格式不正确。如何将此 cmd 命令更改为正确的 C# 字符串格式?
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "cmd.exe";
string AAA= "curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "@xxx.json" "https://some.org/apI/Oauth/token"";
process.StartInfo.Arguments = AAA;
解决方法
您可以在引号字符 \
前使用反斜杠 "
进行转义。使用以下代码。
string AAA = "curl -X POST --header \"Content-Type: application/json\" --header \"Accept: application/json\" -d \"@xxx.json\" \"https://some.org/api/oauth/token\"";
检查小提琴 - https://dotnetfiddle.net/Kfz4ca
,将 @
放在字符串前面并使用双引号。
string AAA = @"curl -X POST --header ""Content-Type: application/json"" --header ""Accept: application/json"" -d ""@xxx.json"" ""https://some.org/api/oauth/token""";
,
string AAA = @"curl -X POST --header ""Content - Type: application / json""--header ""Accept: application / json"" - d ""@xxx.json"" ""https://some.org/api/oauth/token""";