C #: problem with shell commands

I am trying to get the PHP parser to start the page and then return the results to my server, however, when I run the command through my code, it returns nothing. I know that the command is correct, because if I run it manually with the same path, it works fine. Here is my code:

var p = new Process
{
      StartInfo = new ProcessStartInfo("C:\\xampp\\php\\php.exe", path)
      {
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true
      }
};
var output = new StringWriter();
var error = new StringWriter();
p.OutputDataReceived += (sender, args) => output.WriteLine(args.Data);
p.ErrorDataReceived += (sender, args) => error.WriteLine(args.Data);
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit();
if (p.ExitCode != 0)
{
      throw new Exception(string.Format(
          "PHP failed with the following output:{0}{1}",
      /* {0} */ Environment.NewLine,
      /* {1} */ error.GetStringBuilder().ToString()));
}
var res = output.GetStringBuilder().ToString();
Console.WriteLine(res);

EDIT: With this current code, it throws an exception in the code without output.

+3
source share
6 answers

Set the WorkDirectory path

var p = new Process
            {
                StartInfo = new ProcessStartInfo("php", path)
                {
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    WorkingDirectory = workingDir
                }
            };
+5
source

, . . . .

p.Start();
p.WaitForExit();  // New line

OP , .

CMD . PHP . , , .

+5

, :

var p = new Process {
    StartInfo = new ProcessStartInfo("php", path) {
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        UseShellExecute = false,
        CreateNoWindow = true
    }
};
var output = new StringWriter();
var error = new StringWriter();
p.OutputDataReceived += (sender, args) => output.WriteLine(args.Data);
p.ErrorDataReceived += (sender, args) => error.WriteLine(args.Data);
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit();
if (p.ExitCode != 0) {
    throw new Exception(string.Format(
        "PHP failed with the following output:{0}{1}",
        /* {0} */ Environment.NewLine,
        /* {1} */ error.GetStringBuilder().ToString()));
}
var res = output.GetStringBuilder().ToString();
Console.WriteLine(res);
+2

ReadToEnd() , . , Start(), , , .

  • ReadLine() ,
  • p.WaitForExit(), ReadToEnd(), , . , .
+1

p.WaitForExit(); p.Start(); , .

+1

, php cmd? ?

In my experience, running processes through .NET is the worst way to execute another process. fork () on Unix is ​​surprisingly much better. I had so many problems that my advice is that you are STRONGLY considering looking at Process.BeginOutputReadLine () and go asynchronous route in your reading. ReadToEnd and ReadToLine can hang indefinitely, especially if you have a long output to capture.

I would insert an example, but it is quite long.

0
source

Source: https://habr.com/ru/post/1707552/


All Articles