您好我正在尝试使用管道从流程重定向标准输出和标准错误,到目前为止,我可以使用Console.ReadLine()
从服务器到客户端管道发送和接收数据,但我一直在寻找用于发送变量(如标准输出/错误)
NamedPipeClient:
static void client(){
while (true){
var clientStream = new NamedPipeClientStream("ouput");
clientStream.Connect(60);
string line = Console.ReadLine();
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(line);
clientStream.Write(buffer,0,buffer.Length);
if (line.ToLower() == "cerrar")
break;
clientStream.Close();
}
}
服务器管道:
static void server(){
while (true){
var namedPipeServerStream = new NamedPipeServerStream("output");
namedPipeServerStream.WaitForConnection();
byte[] buffer = new byte[255];
namedPipeServerStream.Read(buffer, 0, 255);
string request = ASCIIEncoding.ASCII.GetString(buffer);
Console.WriteLine(request);
request=request.Trim('\0');
if(request.ToLower()=="cerrar")
break;
namedPipeServerStream.Close();
}
}
这就是我开始一个过程的方式:
Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "/bin/bash";
process.StartInfo.Arguments = "-c " + pathFile + " \"";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardOutput.ReadToEnd();
process.WaitForExit();
我们的想法是采用变量output
和error
并通过我的管道发送它们。
提前谢谢。