简体   繁体   English

在C#Process.Start参数中使用单引号

[英]Using single quote in C# Process.Start argument

I'm need to execute Linux command line programs from .Net Core with arguments which include single quote ' 我需要使用包含单引号'参数从.Net Core执行Linux命令行程序'

Two examples: 两个例子:

dpkg-query -W -f=' ${db:Status-Status} ' mariadb*

virsh qemu-agent-command SRV01 '{"execute":"guest-ping"}' 

In C#: 在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();

One example of error message: '''''''''''''''''''dpkg-query: no packages found matching ${db:Status-Status} dpkg-query: no packages found matching ' 错误消息的一个示例: '''''''''''''''''''dpkg-query: no packages found matching ${db:Status-Status} dpkg-query: no packages found matching '

I'm calling perhaps 30 difference programs with arguments without any problems. 我正在调用带有参数的30个差异程序,而没有任何问题。 Only have issue with the single quote 仅单引号有问题

Also tried to use the ProcessStartInfo.ArgumentList and many basic escape tricks but with not success. 还尝试使用ProcessStartInfo.ArgumentList和许多基本的转义技巧,但未成功。

Solution: 解:

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);
        }
    }
}

And another command example: 还有另一个命令示例:

....
//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\"}");
....

Solved with help from TSlivede and jnm2 on the this github.com tread :-) 在此github.com踏板上通过TSlivedejnm2的帮助解决了:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM