我需要点击某菜单,启动某个C++编写的程序,对某文件进行处理,该程序是控制台应用程序,格式如下:
xx.exe filename.txt
同时xx.exe在运行成功时,返回0,不成功时,返回1
那我如何在菜单项的点击按钮中启动该进程,并判断其处理结果是成功还是不成功?

解决方案 »

  1.   

    System.Diagnostics.Process p=new System.Diagnostics.Process();
    p.StartInfo.FileName="xx.exe";
    p.StartInfo.Arguments="filename.txt";
    p.Start();
      

  2.   

    using System.Diagnostics;
    ...
    public class MyProc
    {
    bool openSuccess;

    public StartApp(string filePath)
    {
    Process myProc = new Process();

    try
    {
    myProc.StartInfo.FileName = filePath;
    openSucess = myProc.Start();
    }
    catch (Exception err)
    {
    MessageBox.Show(err.Message);
    }
    }

    public static void Main()
    {
    MyProc myProc = new MyProc();
    myProc.StartApp("C:\\Windows\\Notepad.exe");
    }
    }
      

  3.   

    对了,上面的代码没有写 Arguments。像我楼上的那样就好了
    myProc.StartInfo.Arguments = "filename.txt";
      

  4.   

    Process.ExitCode Property  [C#][C#]
    public int ExitCode {get;}Property Value
    The code that the associated process specified when it terminated.
      

  5.   

    可以这么写吗:
    Process p=new System.Diagnostics.Process();
    p.StartInfo.FileName="xx.exe";
    p.StartInfo.Arguments="filename.txt";
    p.Start();
    if( p.ExitCode == 0 )
       MessageBox.Show("成功");
    else
      MessageBox.Show("失败");可是老是出现异常。
    xx.exe是一个控制台程序,就好象是一个过程一样,过程化的,从头到脚运行。
    我想是不是因为Start函数只是启动进程,我调用p.ExitCode的时候,它还没有运行结束
    那怎么等到进程运行结束时,再判断p.ExitCode?
      

  6.   

    System.Diagnostics.Process p=new System.Diagnostics.Process();
    p.StartInfo.FileName="xx.exe";
    p.StartInfo.Arguments="filename.txt";
    p.Start();
    p.WaitForExit();
    if( p.ExitCode == 0 )
    MessageBox.Show("成功");
    else
    MessageBox.Show("失败");
      

  7.   

    到底是要判断什么成功?
    是否成功启动进程?
    是否成功退出?
    是否成功执行启动的进程的某项功能?如果是第一个,就按我写的。如果第二个就按chenyuming2004(这辈子我算是废了) 写的。如果是第三个就更好写了隐藏控制台窗口我不知道怎么做。还是发个新帖问吧
      

  8.   

    是第三个:是否成功执行启动的进程的某项功能
    在那个控制台应用程序中,如果成功完成该操作
    返回0
    否则返回1
    就是判断其返回值,是不是为0,来判断该操作是否成功完成
    下面这么写应该可以了吧?System.Diagnostics.Process p=new System.Diagnostics.Process();
    p.StartInfo.FileName="xx.exe";
    p.StartInfo.Arguments="filename.txt";
    p.Start();
    p.WaitForExit();
    if( p.ExitCode == 0 )
    MessageBox.Show("成功");
    else
    MessageBox.Show("失败");
      

  9.   

    不好意思
    我是楼主的马甲
    我查MSDN中StartInfo中有个CreateNoWindows好像可以隐藏控制台的窗口
    可是也不行,照样显示