如何在ProcessStartInfo()中使用参数

本文关键字:参数 ProcessStartInfo | 更新日期: 2023-09-27 18:10:17

我正在尝试编写以下代码以在c#中运行python脚本:-

 ProcessStartInfo startinfo = new ProcessStartInfo();
 startinfo.FileName = @"C:'Program Files (x86)'PuTTY'plink.exe";
 startinfo.Arguments = "-ssh username@lpl250srd01  -pw pass /home/abc/dComponents/bin/python eggs/beans/EGG-INFO/scripts/beanstalktop.py";

在上面的"python eggs"之间有一个空格,这意味着我需要执行.py文件,但c#将其作为python目录和鸡蛋作为单独的目录,并抛出一个错误。

有谁能帮我解决这个问题吗?

如何在ProcessStartInfo()中使用参数

必须转义参数文件路径,如:

        ProcessStartInfo info = new ProcessStartInfo("notepad.exe");
        info.Arguments = "'"c:''temp''test test''test.txt'"";
        Process.Start(info);

就这么简单:)

        Process process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                CreateNoWindow = false,
                UseShellExecute = true,
                FileName = "pythonFile.py",
                WindowStyle = ProcessWindowStyle.Normal,
                Arguments = "-sk server -sky exchange -pe -n CN=localhost -ir LocalMachine -is Root -ic MyCA.cer -sr LocalMachine -ss My MyAdHocTestCert.cer"
            }
        };
        process.Start();
        // this line waits till the python file is
        // finished and then resumes the code
        process.WaitForExit();