You cannot do this only with the built-in .NET class Process. It does not support the correct options.
, Windows . UseShellExecute = true; ( Process), , Process () ShellExecuteEx(). Windows Shell , , . , .
, , -, UseShellExecute = true;. false.
: CreateProcess() p/invoke, Process . CREATE_NEW_CONSOLE. , CreateProcess() .
. MSDN , .
, , Process, -. - . , -, , , , , . ( - , , - stdio), - , API-.
( " Windows", " " ) . , . - , . , , , . .
, , (.. PipeDirection.In PipeDirection.Out ) - StandardInput. , . ( ... , , , , :)).
: (ConsoleProxy.exe)
class Program
{
static void Main(string[] args)
{
NamedPipeClientStream pipe = new NamedPipeClientStream(".", args[1],
PipeDirection.InOut, PipeOptions.Asynchronous);
pipe.Connect();
Process process = new Process();
process.StartInfo.FileName = args[0];
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
using (TextReader reader = new StreamReader(pipe))
using (TextWriter writer = new StreamWriter(pipe))
{
Task readerTask = ConsumeReader(process.StandardOutput, writer);
string line;
do
{
line = reader.ReadLine();
if (line != "")
{
line = "proxied write: " + line;
}
process.StandardInput.WriteLine(line);
process.StandardInput.Flush();
} while (line != "");
readerTask.Wait();
}
}
static async Task ConsumeReader(TextReader reader, TextWriter writer)
{
char[] rgch = new char[1024];
int cch;
while ((cch = await reader.ReadAsync(rgch, 0, rgch.Length)) > 0)
{
writer.Write("proxied read: ");
writer.Write(rgch, 0, cch);
writer.Flush();
}
}
}
: (ConsoleApplication1.exe)
class Program
{
static void Main(string[] args)
{
Console.Title = "ConsoleApplication1";
string line;
while ((line = PromptLine("Enter text: ")) != "")
{
Console.WriteLine(" Text entered: \"" + line + "\"");
}
}
static string PromptLine(string prompt)
{
Console.Write(prompt);
return Console.ReadLine();
}
}
:
class Program
{
static void Main(string[] args)
{
NamedPipeServerStream pipe = new NamedPipeServerStream("ConsoleProxyPipe",
PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
Console.Title = "Main Process";
Process process = new Process();
process.StartInfo.FileName = "ConsoleProxy.exe";
process.StartInfo.Arguments = "ConsoleApplication1.exe ConsoleProxyPipe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
pipe.WaitForConnection();
using (TextReader reader = new StreamReader(pipe))
using (TextWriter writer = new StreamWriter(pipe))
{
Task readerTask = ConsumeReader(reader);
string line;
do
{
line = Console.ReadLine();
writer.WriteLine(line);
writer.Flush();
} while (line != "");
readerTask.Wait();
}
}
static async Task ConsumeReader(TextReader reader)
{
char[] rgch = new char[1024];
int cch;
while ((cch = await reader.ReadAsync(rgch, 0, rgch.Length)) > 0)
{
Console.Write(rgch, 0, cch);
}
}
}