.NET Intercepted STDOUT from Python is always empty in Unity - c#

I'm trying to use a Python program to send a continuous stream of information from one program to another, like this question, but in byte form.
pythonPath and pythonScript are just the file locations of the script and python.exe.
C# code
public static void PythonKernel_Test() {
Process pythonProcess = Process.Start(new ProcessStartInfo() {
FileName = pythonPath,
Arguments = pythonScript,
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true
});
Stream pythonStdOut = pythonProcess.StandardOutput.BaseStream;
for (int i = 0; i < 1000; i++) {
byte[] buffer = new byte[256];
pythonStdOut.Read(buffer, 0, 256);
Debug.Log(Encoding.ASCII.GetString(buffer) + Environment.NewLine + BitConverter.ToString(buffer));
}
pythonStdOut.Close();
}
Although this is in Unity, you could just substitute Debug.Log for Console.WriteLine().
However, even though I am spamming stdout with SYNC, nothing appears on the C# side. It does however, appear in the command prompt when ran from shell.
Python code
w = sys.stdout.buffer
while True:
w.write(b"SYNC\n")
sys.stdout.flush()

Here a working sample to try out
public static void PythonKernel_Test()
{
var pi = new ProcessStartInfo
{
FileName = "python",
Arguments = "test.py",
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
};
var pythonProcess = new Process
{
StartInfo = pi,
};
pythonProcess.OutputDataReceived+= (s,e) =>
{
Console.WriteLine("OUT: {0}",e.Data);
};
pythonProcess.ErrorDataReceived+= (s,e) =>
{
Console.WriteLine("ERR: {0}",e.Data);
};
pythonProcess.Start();
pythonProcess.BeginOutputReadLine();
pythonProcess.BeginErrorReadLine();
pythonProcess.WaitForExit();
}
It will read the output asynchronously
Taken from https://stackoverflow.com/a/29753402/1744164
test.py
import sys
while True:
sys.stdout.write(b"SYNC\n")
sys.stdout.flush()

Related

Not possible to set input file in linux command running from .NET Core

I'm running .NET Core app on the linux docker container
When I call the command from the linux terminal it works well:
./darknet detector test -out result.json < data/file-list.txt
But when I start the process from the .NET Core I see error. Process runner method:
public static string RunCommand(string command, string args)
{
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
process.Start();
process.WaitForExit();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
return #$"{output}{Environment.NewLine}-------------------------------{Environment.NewLine}{error}";
}
Calling code:
string args = #$"detector test -out result.json < data/file-list.txt";
string output = ProcessRunner.RunCommand("./darknet", args);
Here is the part of the output:
Cannot load image "<"
STB Reason: can't fopen
How to fix it?
You can write the process's standard input once you set the RedirectStandartInput to true while starting your process. Here is an example how to write :
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "./ConsoleApp1.exe",
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true, // here you need
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
using var file = File.OpenRead("./1.txt");
using var reader = new StreamReader(file);
while (true)
{
var line = await reader.ReadLineAsync();
if (string.IsNullOrWhiteSpace(line)) break; // you can use some other stoping decision
await process.StandardInput.WriteLineAsync(line);
}

Getting the error of a Powershell Script within c# and outputting it with conditional statement?

I have a console application and a method that executes a PowerShell script within the console application. So I'm trying to grab an error text that it outputs in the application and do something with it.
Example/What I'm trying to do:
If Error.contains("Object")
{
// do something here
}
Here is my current method
public void ExecutePowershellScript()
{
var file = #"C:\Path\filename.ps1";
var start = new ProcessStartInfo()
{
FileName = "powershell.exe",
Arguments = $"-NoProfile -ExecutionPolicy unrestricted -file \"{file}\"",
UseShellExecute = false
};
Process.Start(start);
}
Process.start: how to get the output?
When you create your Process object set StartInfo appropriately:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "program.exe",
Arguments = "command line arguments to your executable",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
then start the process and read from it:
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
string line = proc.StandardOutput.ReadLine();
// do something with line
}
You can use int.Parse() or int.TryParse() to convert the strings to numeric values. You may have to do some string manipulation first if there are invalid numeric characters in the strings you read.
You can set RedirectStandardError = true and access any errors from process.StandardError
public static void ExecutePowershellScript()
{
var file = #"C:\Path\filename.ps1";
var start = new ProcessStartInfo()
{
FileName = "powershell.exe",
Arguments = $"-NoProfile -ExecutionPolicy unrestricted -file \"{file}\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using Process process = Process.Start(start);
string output = process.StandardOutput.ReadToEnd();
string errors = process.StandardError.ReadToEnd();
}
Okay, scratch the above suggestion.
After being corrected by mklement0,
This is a perfectly reasonable attempt, but, unfortunately, it can lead to hangs (while waiting for one's stream end, the other, when exceeding the buffer size, may cause process execution to block). If you need to capture both streams, you must collect the output from one of them via events. – mklement0
I changed the solution to use the ErrorDataReceived event
public static async Task ExecutePowershellScript()
{
var file = #"C:\Path\filename.ps1";
var start = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = $"-NoProfile -ExecutionPolicy unrestricted -file \"{file}\"",
UseShellExecute = false,
// redirect standard error stream to process.StandardError
RedirectStandardError = true
};
using var process = new Process
{
StartInfo = start
};
// Subscribe to ErrorDataReceived event
process.ErrorDataReceived += (sender, e) =>
{
// code to process the error lines in e.Data
};
process.Start();
// Necessary to start redirecting errors to StandardError
process.BeginErrorReadLine();
// Wait for process to exit
await process.WaitForExitAsync();
}
start.Start();
while (!start.StandardOutput.EndOfStream)
{
string line = start.StandardOutput.ReadLine();
}

How to execute a shell command across platforms in .NET Core?

In my .NET Core Console app, I receive multiple commands in form of an array of string, and would like to execute them as console command (and showing their output in my own app if possible but not hard requirement).
At first, I tried to parse each command to separate their name and arguments and put them in ProcessStartInfo. However, some command does not work (even simple commands like echo "Hello").
Now I switched to call Powershell instead like this:
static IEnumerable<ProcessStartInfo> ParseCommands(string[] args)
{
return args
.Skip(1)
.Select(q => new ProcessStartInfo()
{
FileName = "powershell",
Arguments = q,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
}).ToList();
}
static void RunCommand(ProcessStartInfo processInfo)
{
Console.WriteLine($"{processInfo.Arguments}");
var process = new Process()
{
StartInfo = processInfo,
};
process.Start();
while (!process.StandardOutput.EndOfStream)
{
Console.WriteLine(process.StandardOutput.ReadLine());
}
process.WaitForExit();
}
The problem is I don't think this one can run on Linux or MacOS. Is there any "standard" way to tell my app to "run this as if it's a console command"?
This is my current code by using the Platform to determine the console command, feel free to tell me if there is a better way:
static IEnumerable<ProcessStartInfo> ParseCommands(string[] args)
{
var argsPrepend = "";
var shellName = "/bin/bash";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
shellName = "cmd";
argsPrepend = "/c ";
}
return args
.Skip(1)
.Select(q => new ProcessStartInfo()
{
FileName = shellName,
Arguments = argsPrepend + q,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
}).ToList();
}
static void RunCommand(ProcessStartInfo processInfo)
{
Console.WriteLine($"{processInfo.Arguments.Substring(processInfo.FileName == "cmd" ? 3 : 0)}");
var process = new Process()
{
StartInfo = processInfo,
};
process.Start();
while (!process.StandardOutput.EndOfStream)
{
Console.WriteLine(process.StandardOutput.ReadLine());
}
process.WaitForExit();
}

Not able to send commands to cmd.exe process

I am trying to send commands to an open cmd.exe process using StandardInput.WriteLine(str), however none of the commands seem to be sent. First I open a process, with a global variable p (Process p).
p = new Process()
{
StartInfo = {
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
FileName = #"cmd.exe",
Arguments = "/C" //blank arguments
}
};
p.Start();
p.WaitForExit();
After, I try to send a command using a simple method, that logs the result in a text box.
private void runcmd(string command)
{
p.StandardInput.WriteLine(command);
var output = p.StandardOutput.ReadToEnd();
TextBox1.Text = output;
}
Right now I am testing it with DIR, but var output shows up as null, which results in no output. Is there a better way to send a command to the open cmd.exe process?
I could never get it to work with synchronous reads of stdout without closing stdin, but it does work with async reading for stdout/stderr. No need to pass in /c, you only do that when passing in a command through the arguments; you are not doing this though, you are sending the command directly to the input.
var p = new Process()
{
StartInfo = {
CreateNoWindow = false,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
FileName = #"cmd.exe"}
};
p.OutputDataReceived += (sender, args1) => Console.WriteLine(args1.Data);
p.ErrorDataReceived += (sender, args1) => Console.WriteLine(args1.Data);
p.Start();
p.BeginOutputReadLine();
p.StandardInput.WriteLine("dir");
p.StandardInput.WriteLine("cd e:");
p.WaitForExit();
Console.WriteLine("Done");

Process with ProcessWindowStyle.Hidden still shows Press any key to exit?

I have this:
var startInfo = new ProcessStartInfo
{
FileName = _pathToExe,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
WorkingDirectory = FilepathHelper.GetFolderFromFullPath(_pathToExe),
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
};
try
{
using (_proc = Process.Start(startInfo))
{
_proc.EnableRaisingEvents = true;
_proc.ErrorDataReceived += proc_DataReceived;
_proc.OutputDataReceived += proc_DataReceived;
_proc.BeginErrorReadLine();
_proc.BeginOutputReadLine();
var myStreamWriter = _proc.StandardInput;
var allArgs = "";
foreach (var arg in _args)
allArgs += arg + Environment.NewLine;
myStreamWriter.Write(allArgs);
_proc.WaitForExit();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
And I am executing an *.exe that someone else wrote. For this particular *.exe - even though you can clearly see above that I have set ProcessWindowStyle.Hidden, I still see a black window appear with the words "Press any key to exit.". This *.exe - if I run from the command line (instead of calling it from my C# code) produces a tremendous amount of console output text. I do not see this output text when I run my C# code, which is what I want and also means the redirection is working.
I checked and the process is finished - it's as if the command window itself is adding this extra (undesirable) step.
Has anyone encountered this before and if so how can I get rid of this?
It seems that the program you are starting is calling system("PAUSE") at the end thus spawning a new process which prints the "Press Any Key to Continue..." message and waits for user input. I cannot reproduce the exact situation of yours but you can try this.
var startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true,
};
try
{
Process _proc;
using (_proc = Process.Start(startInfo))
{
_proc.EnableRaisingEvents = true;
_proc.BeginErrorReadLine();
_proc.BeginOutputReadLine();
var myStreamWriter = _proc.StandardInput;
myStreamWriter.WriteLine("D:\\your.exe"); //write your.exe to cmd and press enter :)
_proc.WaitForExit();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
From Document:
To use System.Diagnostics.ProcessWindowStyle.Hidden, the system.Diagnostics.ProcessStartInfo.UseShellExecute property must be true.

Categories

Resources