赞
踩
调用jar包,首先你得先开发一个jar包,可以自己通过命令:
java -jar xxxxx.jar param1 正常调用。
调用代码:
private void button3_Click(object sender, EventArgs e) { Process p = new Process(); //设置要启动的应用程序 p.StartInfo.FileName = "cmd.exe"; //是否使用操作系统shell启动 p.StartInfo.UseShellExecute = false; // 接受来自调用程序的输入信息 p.StartInfo.RedirectStandardInput = true; //输出信息 p.StartInfo.RedirectStandardOutput = true; // 输出错误 p.StartInfo.RedirectStandardError = true; //不显示程序窗口 p.StartInfo.CreateNoWindow = true; p.StartInfo.WindowStyle = ProcessWindowStyle.Normal; //启动程序 p.Start(); //向cmd窗口发送输入信息 p.StandardInput.WriteLine("java -jar D:\\360MoveData\\Users\\Administrator\\Desktop\\speak.jar 中国国家" + "&exit"); p.StandardInput.AutoFlush = true; //获取输出信息 string strOuput = p.StandardOutput.ReadToEnd(); Console.WriteLine(strOuput); //等待程序执行完退出进程 p.WaitForExit(); p.Close(); }
调用Python脚本也一样,开发好Python脚本,首先得能独立运行,自己测好后,在C#里调用,
代码:
string pythonFilePath = Directory.GetCurrentDirectory() + "\\脚本\\自动出报告\\WriteHtml_地图.py"; Process proc = null; try { proc = new Process(); //proc.StartInfo.WorkingDirectory = pythonFilePath; proc.StartInfo.FileName = @"C:\Windows\System32\cmd.exe"; proc.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息 proc.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息 proc.StartInfo.UseShellExecute = false; //要重定向 IO 流,Process 对象必须将 UseShellExecute 属性设置为 False proc.StartInfo.CreateNoWindow = true; //不显示程序窗口 proc.Start(); string cmd = "python " + pythonFilePath + " " + tableName + " " + rangeType + " " + rangeName + " " + yiDong + " " + lianTong + " " + dianXin + "&exit"; Logger.RecordInfo(cmd); proc.StandardInput.WriteLine(cmd); proc.StandardInput.AutoFlush = true; //获取cmd窗口的输出信息 string output = proc.StandardOutput.ReadToEnd(); Logger.RecordInfo(output); proc.WaitForExit(); proc.Close(); if (output.Contains("执行结束")) { MessageBox.Show(type + "分析完成!!!"); this.Close(); } } catch (Exception ex) { Logger.RecordInfo(ex.StackTrace.ToString()); Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString()); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。