PB调用C#写的EXE,如何获取返回其执行程序的字符串值?
PB我没接触过,但所做的项目中涉及到:PB调用C#写的EXE。这个C#的EXE是我们公司写的,PB是另外公司,需要让对方来调用我们的EXE,调用时候规定了XML输入参数,执行完EXE后,PB需要获取EXE给他的XML格式返回值,在调用过程中,我们公司会有一些业务流程窗体交互。
请高手来帮忙解决!
C#代码EXE服务执行程序(ConsoleApp.exe)样例代码的如下:
static void Main(string[] args)
{
string procName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
File.AppendAllText("D:\\systemtool.txt", procName + "\n");
if ((System.Diagnostics.Process.GetProcessesByName(procName)).GetUpperBound(0) == 0)
{
File.AppendAllText("D:\\systemtool.txt", "Bussiness handle" + "\n");
//接收传过来的XML格式字符串参数
string paramXML = "";
if (!string.IsNullOrEmpty(args[0]))
{
paramXML = args[0];
File.AppendAllText("D:\\systemtool.txt", "Input XML parameters:" + paramXML + "\n");
XmlDocument oXmlDoc = new XmlDocument();
try
{
oXmlDoc.LoadXml(paramXML);
XmlNode root = oXmlDoc.DocumentElement;
XmlNodeList nodes = root.ChildNodes;
foreach (XmlNode node in nodes)
{
File.AppendAllText("D:\\systemtool.txt", node.Name + "->" + node.InnerText + "\n\n");
}
//调用其他业务窗体代码
.................
}
catch (Exception error)
{
File.AppendAllText("D:\\systemtool.txt", "Exception error->" + error.Message + "\n");
throw new Exception(error.Message);
}
finally
{
oXmlDoc = null;
}
}
//返回值
string returnParam = "<Root><RetValue>1</RetValue></Root>";
Console.Write(returnParam);//将返回值写入到标准的控制台输出。
}
C#调用EXE执行程序样例,如下:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
//外部Console服务程序物理路径
p.StartInfo.FileName = @"F:\ConsoleApp.exe";
p.StartInfo.CreateNoWindow = true;
//启动参数
p.StartInfo.Arguments = "<Root><Inparam>0</Inparam></Root>";
p.Start();
p.WaitForExit();
//返回值
string returnValue = p.StandardOutput.ReadToEnd();