这个控制台程序是用其它语言写的,我想在C#中调用它,并传入一些内容,然后得到该控制台输出的数据,请问怎么做,谢谢

解决方案 »

  1.   

                    //实例一个Process类,启动一个独立进程 
                Process p = new Process(); 
                p.StartInfo.FileName = exePath;          //设定程序名 
                p.StartInfo.Arguments = command;    //设定程序执行参数 
                p.StartInfo.UseShellExecute = false;        //关闭Shell的使用 
                p.StartInfo.RedirectStandardInput = true;  //复位向标准输入 
                p.StartInfo.RedirectStandardOutput = true;  //复位向标准输出 
                p.StartInfo.RedirectStandardError = true;  //复位向错误输出 
                p.StartInfo.CreateNoWindow = false;          //设置不显示窗口             p.Start();  //启动             //p.StandardInput.WriteLine(command);      //输入要执行的命令 
                //p.StandardInput.WriteLine("exit");        //Exit            return p.StandardOutput.ReadToEnd();
      

  2.   

    System.Diagnostics.Process.Start("CMD.EXE", @"/C""你的外部程序名.EXE 命令行参数(如果有的话) <INPUT >OUTPUT""");
    其中 INPUT 为输入,OUTPUT 为输出。
      

  3.   

                    //实例一个Process类,启动一个独立进程 
                Process p = new Process(); 
                p.StartInfo.FileName = exePath;          //设定程序名 
                p.StartInfo.Arguments = command;    //设定程序执行参数 
                p.StartInfo.UseShellExecute = false;        //关闭Shell的使用 
                p.StartInfo.RedirectStandardInput = true;  //复位向标准输入 
                p.StartInfo.RedirectStandardOutput = true;  //复位向标准输出 
                p.StartInfo.RedirectStandardError = true;  //复位向错误输出 
                p.StartInfo.CreateNoWindow = false;          //设置不显示窗口             p.Start();  //启动             p.StandardInput.WriteLine(command);      //输入要执行的命令 
                String output = p.StandardOutput.ReadToEnd();
                p.StandardInput.WriteLine("exit");        //Exit           return output;
    这样比较好看= =
      

  4.   

    不行呀,是这样的,启动这个控制台EXE后,里面出现“请输入学号:”字样,然后输入学号之后,又出现“请输入姓名:”字样,
    输入之后就会输出一个字符串True或False,我如何拿到这个输出的字符串呢?
      

  5.   

    p.StandardInput.WriteLine(学号+“\r\n”);
    p.StandardInput.WriteLine(姓名+“\r\n”);
    output=控制台中所有内容
    可考虑
    if(output.IdexOf("True")>-1)
    true
    else
    false
    或者截取IndexOf(姓名),Length-LastIndexOf("\r\n")
      

  6.   

    像你所说的这样,最后得到output的内容是:请输入学号:请输入姓名:
      

  7.   

    那么那个true或false不是同一个出口?
      

  8.   

    是这样的:
    情况一:
    请输入学号:123456
    请输入姓名:yy
    true情况二:
    请输入学号:123456
    请输入姓名:gg
    false
      

  9.   

    比如控制台
    using System;
    using System.Collections.Generic;
    using System.Text;namespace t1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Write("请输入学号:");
                String a=Console.ReadLine();
                Console.Write("请输入姓名:");
                String b=Console.ReadLine();
                if (a.Length > 5)
                    Console.Write("true");
                else
                    Console.Write("false");
            }
        }
    }
    调用
                Process p = new Process();
                p.StartInfo.FileName = "d:\\t1.exe";          //设定程序名 
                p.StartInfo.Arguments = null;    //设定程序执行参数 
                p.StartInfo.UseShellExecute = false;        //关闭Shell的使用 
                p.StartInfo.RedirectStandardInput = true;  //复位向标准输入 
                p.StartInfo.RedirectStandardOutput = true;  //复位向标准输出 
                p.StartInfo.RedirectStandardError = true;  //复位向错误输出 
                p.StartInfo.CreateNoWindow = false;          //设置不显示窗口             p.Start();  //启动             p.StandardInput.WriteLine(this.textBox1.Text);      //输入要执行的命令 
                p.StandardInput.WriteLine("000000");            String output = p.StandardOutput.ReadToEnd();
                p.StandardInput.WriteLine("exit");        //Exit            this.label1.Text= output;this.textBox1.Text="1111111111";返回 请输入学号:请输入姓名:true
    this.textBox1.Text="11";返回 请输入学号:请输入姓名:false
    但是如果你控制台程序不是这样的出口- -……
      

  10.   

    1、 p.StartInfo.CreateNoWindow = false;          //设置不显示窗口 
    <<变成true就能隐藏2、直接读貌似也可以……,就是麻烦点3、应该是能拿到的,和你直接用命令行一样
      

  11.   

    2、System.IO.StreamReader reader = p.StandardOutput;
                string line = reader.ReadLine();//每次读取一行
    重新定向下