Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中shell执行文件?_C#_.net - Fatal编程技术网

如何在C#中shell执行文件?

如何在C#中shell执行文件?,c#,.net,C#,.net,我像往常一样尝试使用Process类,但没有成功。我所做的只是试图运行一个Python文件,就像有人双击它一样 可能吗 编辑: 示例代码: string pythonScript = @"C:\callme.py"; string workDir = System.IO.Path.GetDirectoryName ( pythonScript ); Process proc = new Process ( ); proc.StartInfo.WorkingDirectory = workDi

我像往常一样尝试使用Process类,但没有成功。我所做的只是试图运行一个Python文件,就像有人双击它一样

可能吗

编辑:

示例代码:

string pythonScript = @"C:\callme.py";

string workDir = System.IO.Path.GetDirectoryName ( pythonScript );

Process proc = new Process ( );
proc.StartInfo.WorkingDirectory = workDir;
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.FileName = pythonScript;
proc.StartInfo.Arguments = "1, 2, 3";

我没有得到任何错误,但是脚本没有运行。手动运行脚本时,我会看到结果。

应该可以工作。如果没有,你会发布你的代码和你得到的错误吗?

这是我从C#执行python脚本的代码,带有重定向的标准输入和输出(我通过标准输入传递信息),从web上某个地方的示例复制而来。正如您所看到的,Python位置是硬编码的,可以重构

    private static string CallPython(string script, string pyArgs, string workingDirectory, string[] standardInput)
    {

        ProcessStartInfo startInfo;
        Process process;

        string ret = "";
        try
        {

            startInfo = new ProcessStartInfo(@"c:\python25\python.exe");
            startInfo.WorkingDirectory = workingDirectory;
            if (pyArgs.Length != 0)
                startInfo.Arguments = script + " " + pyArgs;
            else
                startInfo.Arguments = script;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = true;
            startInfo.RedirectStandardInput = true;

            process = new Process();
            process.StartInfo = startInfo;


            process.Start();

            // write to standard input
            foreach (string si in standardInput)
            {
                process.StandardInput.WriteLine(si);
            }

            string s;
            while ((s = process.StandardError.ReadLine()) != null)
            {
                ret += s;
                throw new System.Exception(ret);
            }

            while ((s = process.StandardOutput.ReadLine()) != null)
            {
                ret += s;
            }

            return ret;

        }
        catch (System.Exception ex)
        {
            string problem = ex.Message;
            return problem;
        }

    }

您最后忘记了proc.Start()。如果调用Start(),您拥有的代码应该可以工作。

您可以共享您的代码吗?您所说的“不工作”是什么意思?是System.Diagnostics.Process类吗?e、 是的,很抱歉我没有发布代码。你真的调用了proc.Start()吗?上面的代码中没有。谢谢,您知道如何以编程方式获取python位置吗?