隐藏

c#调用exe捕获返回值

发布:2018/12/24 20:42:22作者:管理员 来源:本站 浏览次数:1291

c#源码例子如下:
调用方法:
string baseName = System.IO.Directory.GetCurrentDirectory();
            // baseName+"/"
            // string fileName = @"C:\Users\59930\Desktop\20170605\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1\bin\x86\Debug\WindowsFormsApp1.exe";
            string fileName = baseName + @"\CardRead.exe";
            string para = "1.exe " + code;
          
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = fileName;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.Arguments = para;//参数以空格分隔,如果某个参数为空,可以传入””  
            p.Start();
            p.WaitForExit();
            string output = p.StandardOutput.ReadToEnd();



调用的exe 返回值中写
Console.Write(mmma);



 private void ProgressCheck(object sender, EventArgs e)

        {
            string strCheck = System.Environment.CurrentDirectory + "\\ProgressCheck\\ProgressCheck.exe";
            string TaskPollExePath = System.Environment.CurrentDirectory + "\\TaskPoll.exe";
            string CreFusXmlExePath = System.Environment.CurrentDirectory + "\\CreFusXml.exe";
            string CoordConvertTaskExePath = System.Environment.CurrentDirectory + "\\CoordCvt\\CoordConvertTask.exe";
            string GCSConvertExePath = System.Environment.CurrentDirectory + "\\CoordCvt\\GCSConvert.exe";
            string CreFusCheckPrjExePath = System.Environment.CurrentDirectory + "\\FusCheck\\CreFusCheckPrj.exe";
            string ProTskExePath = System.Environment.CurrentDirectory + "\\FusCheck\\TskExe\\ProTsk.exe";
            string MDomSupQuaChkExePath = System.Environment.CurrentDirectory + "\\FusCheck\\KnlExe\\MDomSupQuaChk.exe";

            Process process1 = CreateProcessTasks(strCheck, TaskPollExePath, true);
            process1.Start();
            process1.WaitForExit();
            if (process1.ExitCode!= 0)
            {
                MessageBox.Show(TaskPollExePath, "程序异常");
                return;
            }

            Process process2 = CreateProcessTasks(strCheck, CreFusXmlExePath, true);
            process2.Start();
            process2.WaitForExit();
            if (process2.ExitCode != 0)
            {
                MessageBox.Show(CreFusXmlExePath, "程序异常");
                return;
            }

            Process process3 = CreateProcessTasks(strCheck, CoordConvertTaskExePath, true);
            process3.Start();
            process3.WaitForExit();
            if (process3.ExitCode != 0)
            {
                MessageBox.Show(CoordConvertTaskExePath, "程序异常");
                return;
            }

            Process process4 = CreateProcessTasks(strCheck, GCSConvertExePath, true);
            process4.Start();
            process4.WaitForExit();
            if (process4.ExitCode != 0)
            {
                MessageBox.Show(GCSConvertExePath, "程序异常");
                return;
            }

            Process process5 = CreateProcessTasks(strCheck, CreFusCheckPrjExePath, true);
            process5.Start();
            process5.WaitForExit();
            if (process5.ExitCode != 0)
            {
                MessageBox.Show(CreFusCheckPrjExePath, "程序异常");
                return;
            }

            Process process6 = CreateProcessTasks(strCheck, ProTskExePath, true);
            process6.Start();
            process6.WaitForExit();
            if (process6.ExitCode != 0)
            {
                MessageBox.Show(ProTskExePath, "程序异常");
                return;
            }

            Process process7 = CreateProcessTasks(strCheck, MDomSupQuaChkExePath, true);
            process7.Start();
            process7.WaitForExit();
            if (process7.ExitCode != 0)
            {
                MessageBox.Show(MDomSupQuaChkExePath, "程序异常");
                return;
            }

            MessageBox.Show("程序无异常","提示");
            return;
        }

        private Process CreateProcessTasks(string exe_path, string str_arguments, bool b_create_win = true)
        {
            Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = exe_path;
            process.StartInfo.Arguments = str_arguments;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = false;  //true
            process.StartInfo.RedirectStandardOutput = false;  //true
            process.StartInfo.RedirectStandardError = false;
            process.StartInfo.CreateNoWindow = b_create_win;
            //process.Start();
            return process;
        }