【问题标题】:Execute a console application in c#? [duplicate]在 C# 中执行控制台应用程序? [复制]
【发布时间】:2011-06-24 13:01:40
【问题描述】:

可能重复:
How to Run a C# console application with the console hidden.

如何通过类库在后台运行控制台应用程序来执行任务,然后告诉我的方法它已完成处理?

【问题讨论】:

  • 通过控制台应用程序,我假设您的意思是 dos 窗口可执行文件?
  • 您希望它异步返回还是等待应用完成?
  • 001,您应该尝试将人们给您的更多答案标记为“已接受”。这对遇到您的问题的其他人有帮助,也对帮助您的人给予应有的赞誉。

标签: c# class console console-application class-library


【解决方案1】:

您可以为此使用Process 类。

这是一个例子:

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "your application path";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();

还有一个HasExited 属性来检查进程是否已完成。

你可以这样使用它:

if (p.HasExited)...

或者您可以将 EventHandler 绑定到 Exited 事件。

【讨论】:

    【解决方案2】:

    如果您可以等待您的应用程序完成运行,请按照 Shekhar 的回答使用 proc.WaitForExit()。如果您希望它在后台运行而不是等待,请使用 Exited 事件。

    Process proc =
        new Process
        {
            StartInfo =
            {
                FileName = Application.StartupPath +  @"your app name",
                Arguments = "your arguments"
            }
        };
    
    proc.Exited += ProcessExitedHandler;
    
    proc.Start();
    

    完成后,您可以检查错误代码:

    if (proc.ExitCode == 1)
    {
        // successful
    }
    else
    {
        // something else
    }
    

    【讨论】:

      【解决方案3】:

      我在这里有点冒险,但我认为您的意思是完全隐藏控制台应用程序窗口。

      这样的话,你可以通过一些P/Invoking来实现。

      我撒谎了。我发布的原始代码只是禁用了托盘中的“X”按钮。很抱歉造成混乱...

      WinForms.ShowWindow(consoleWindow, NativeConstants.SW_HIDE)
      
          [DllImport("user32.dll")]
          public static extern Boolean ShowWindow(IntPtr hWnd, Int32 show);
      

      还有这里的 P/Invoke 语句:

          /// <summary>
          ///     The EnableMenuItem function enables, disables, or grays the specified menu item.
          /// </summary>
          /// <param name="hMenu"></param>
          /// <param name="uIDEnableItem"></param>
          /// <param name="uEnable"></param>
          /// <returns></returns>
          [DllImport("user32.dll")]
          public static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
      
          [DllImport("kernel32.dll")]
          public static extern IntPtr GetConsoleWindow();
      
          /// <summary>
          ///     The GetSystemMenu function allows the application to access the window menu (also known as the system menu or the control menu) for copying and modifying.
          /// </summary>
          /// <param name="hWnd"></param>
          /// <param name="bRevert"></param>
          /// <returns></returns>
          [DllImport("user32.dll")]
          public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
      

      原码

          private static IntPtr hWndConsole = IntPtr.Zero;
          private static IntPtr hWndMenu = IntPtr.Zero;
      
          public static void Main(string[] args)
          {
              hWndConsole = WinForms.GetConsoleWindow();
              if (hWndConsole != IntPtr.Zero)
              {
                  hWndMenu = WinForms.GetSystemMenu(hWndConsole, false);
      
                  if (hWndMenu != IntPtr.Zero)
                  {
                      WinForms.EnableMenuItem(hWndMenu, NativeConstants.SC_CLOSE, (uint)(NativeConstants.MF_GRAYED));
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-21
        • 1970-01-01
        • 2014-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多