zoukankan      html  css  js  c++  java
  • Web应用程序或者WinForm程序 调用 控制台应用程序及参数传递

    有时候在项目中,会调用一个控制台应用程序去处理一些工作。
    那在我们的程序中要怎么样做才能调用一个控制台应用程序并将参数传递过去,控制台程序执行完后,我们的程序又怎样获取返回值?
    代码如下:
    调用代码:

                try
                {
                    Process pro = new Process();
                    //控制台应用程序所在目录
                    pro.StartInfo.FileName = @"C:UsersAdministratorDesktopWindowsFormsApplication1ConsoleApplication1inDebugConsoleApplication1.exe";
                    pro.StartInfo.UseShellExecute = false;
                    pro.StartInfo.CreateNoWindow = false//是/否 不显示窗口
                    //是否将应用程序的输出写入 System.Diagnostics.Process.StandardOutput 流中。
                    //如果设置为true,则可以使用 pro.StandardOutput.ReadLine()来获取cmd 窗口里的输出内容,
                    //并且 cmd窗口里不在出现输出。
                    pro.StartInfo.RedirectStandardOutput = true;
                    //命令行参数,以空格隔开,即 控制台应用程序的Main方法的参数
                    pro.StartInfo.Arguments = "123 456";
                    pro.Start();
                    //显示控制台程序的输出内容
                    this.txtConsoleValue.Text += "
    " + pro.StandardOutput.ReadLine();
     
                    //调用控制台程序的返回值,
                    //当控制程序的Main放有返回值时,可以通过 ExitCode 获取
                    int result = pro.ExitCode;
     
                }
                catch (Exception)
                {
                    throw;
                }

    控制台 代码:
            static void Main(string[] args)
            {
                if (args.Length > 0)
                {
                    Console.WriteLine("参数1:" + args[0] + "  参数2:" + args[1]);
                }
                //当调用方,将pro.StartInfo.RedirectStandardOutput设置为false时,可以进行输入交互,
                Console.WriteLine("请输入参数:");
                string param = Console.ReadLine();
                Console.WriteLine("您输入的参数为:" + param);
                System.Threading.Thread.Sleep(3000);
            }
  • 相关阅读:
    LeetCode 88. Merge Sorted Array
    LeetCode 75. Sort Colors
    LeetCode 581. Shortest Unsorted Continuous Subarray
    LeetCode 20. Valid Parentheses
    LeetCode 53. Maximum Subarray
    LeetCode 461. Hamming Distance
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode 976. Largest Perimeter Triangle
    LeetCode 1295. Find Numbers with Even Number of Digits
    如何自学并且系统学习计算机网络?(知乎问答)
  • 原文地址:https://www.cnblogs.com/joey0210/p/3238637.html
Copyright © 2011-2022 走看看