我想开发一个c#应用程序(a.exe)
,它输入到另一个c#应用程序(b.exe)
b.exe
不接受任何论据。
b.exe -call or b.exe call
等不起作用
当我从cmd打开b.exe
时,会采用以下参数:
b:call
b:command 2
....
b:command n
如何从b.exe
提供a.exe
?
我已经习惯了但是没有用。
var psi = new ProcessStartInfo("b.exe")
{
RedirectStandardInput = true,
RedirectStandardOutput = false,
UseShellExecute = false
};
var p = new Process { StartInfo = psi };
p.Start();
p.StandardInput.WriteLine("call");
答案 0 :(得分:-1)
以下是我从您的问题中理解的内容:
a.exe
启动b.exe
a.exe
使用命令行参数或标准输入将命令/请求传递给b.exe
b.exe
收到命令/请求,并通过StandardOutput传递信息使用命令行参数有点简单,除非您需要为每个请求/命令多次运行b.exe
。
// b.exe
static void Main(string[] args)
{
// using command line arguments
foreach (var arg in args)
ProcessArg(arg);
// if using input, then use something like if processing line by line
// string line = Console.ReadLine();
// while (line != "exit" || !string.IsNullOrEmpty(line))
// ProcessArg(line);
// or you can read the input stream
var reader = new StreamReader(Console.OpenStandardInput());
var text = reader.ReadToEnd();
// TODO: parse the text and find the commands
}
static void ProcessArg(string arg)
{
if (arg == "help") Console.WriteLine("b.exe help output");
if (arg == "call") Console.WriteLine("b.exe call output");
if (arg == "command") Console.WriteLine("b.exe command output");
}
// a.exe
static void Main(string[] args)
{
// Testing -- Send help, call, command --> to b.exe
var psi = new ProcessStartInfo("b.exe", "help call command")
{
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
};
var p = new Process() { StartInfo = psi };
p.ErrorDataReceived += p_ErrorDataReceived;
p.OutputDataReceived += p_OutputDataReceived;
p.Start();
p.BeginErrorReadLine();
p.BeginOutputReadLine();
// if sending data through standard input (May need to use Write instead
// of WriteLine. May also need to send end of stream (0x04 or ^D / Ctrl-D)
StreamWriter writer = p.StandardInput;
writer.WriteLine("help");
writer.WriteLine("call");
writer.WriteLine("exit");
writer.Close();
p.WaitForExit();
}
static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.Error.WriteLine("a.exe error: " + e.Data ?? "(null)");
}
static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("a.exe received: " + e.Data ?? "(null)");
}
查看OutputDataReceived和ErrorDataReceived事件以及这些方法中的MSDN示例。
一个考虑因素是您无法管理使用管理权限运行的程序的输出。您需要保存输出,然后将输出传递给其他程序。
我找到了一个有用的CSharpTest库,其中包含ProcessRunner类。例如,浏览ProcessRunner的“InternalStart”方法。