[英]Using single quote in C# Process.Start argument
我需要使用包含單引號'
參數從.Net Core執行Linux命令行程序'
兩個例子:
dpkg-query -W -f=' ${db:Status-Status} ' mariadb*
virsh qemu-agent-command SRV01 '{"execute":"guest-ping"}'
在C#中:
Process proc = new System.Diagnostics.Process();
ProcessStartInfo pi = new ProcessStartInfo("dpkg-query");
pi.Arguments = "-W -f=' ${db:Status-Status} ' mariadb*";
proc.StartInfo = pi;
proc.Start();
錯誤消息的一個示例: '''''''''''''''''''dpkg-query: no packages found matching ${db:Status-Status} dpkg-query: no packages found matching '
我正在調用帶有參數的30個差異程序,而沒有任何問題。 僅單引號有問題
還嘗試使用ProcessStartInfo.ArgumentList
和許多基本的轉義技巧,但未成功。
解:
using System;
using System.Diagnostics;
namespace Exe
{
class Program
{
static void Main(string[] args)
{
Process proc = new System.Diagnostics.Process();
//In the Linux shell: dpkg-query -W -f=' ${db:Status-Status} ' mariadb*:
ProcessStartInfo pi = new ProcessStartInfo("dpkg-query");
pi.ArgumentList.Add("-W");
pi.ArgumentList.Add("-f= ${db:Status-Status} ");
pi.ArgumentList.Add("mariadb*");
pi.UseShellExecute = false;
proc.StartInfo = pi;
proc.Start();
do { System.Threading.Thread.Sleep(50); } while (proc.HasExited == false);
Environment.Exit(0);
}
}
}
還有另一個命令示例:
....
//In the Linux shell: virsh qemu-agent-command SRV04 '{"execute":"guest-ping"}'
ProcessStartInfo pi = new ProcessStartInfo("virsh");
pi.ArgumentList.Add("qemu-agent-command");
pi.ArgumentList.Add("SRV03");
pi.ArgumentList.Add("{\"execute\":\"guest-ping\"}");
....
在此github.com踏板上通過TSlivede和jnm2的幫助解決了:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.