【问题标题】:How to close Console window from windows form applications in C#?如何从 C# 中的 Windows 窗体应用程序关闭控制台窗口?
【发布时间】:2018-01-15 03:09:49
【问题描述】:

我有一个c#程序,目前我运行2个窗口,第一个是窗体窗口,第二个是用于调试的控制台窗口。

根据以下内容创建的控制台窗口:

Create a Windows Form project...

Then: Project Properties -> Application -> Output Type -> Console Application

如何通过单击按钮从表单中关闭控制台窗口?

编辑:

我尝试执行以下操作,但这只是关闭表单窗口..

private void button1_Click(object sender, EventArgs e)
{
    System.Environment.Exit(0);
}

编辑 2: 前面的代码只在下面的代码执行之前有效。

        using (process = new Process())
    {
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
        process.StartInfo.WorkingDirectory = @"C:\";
        process.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");

        // Redirects the standard input so that commands can be sent to the shell.
        process.StartInfo.RedirectStandardInput = true;
        // Runs the specified command and exits the shell immediately.
        //process.StartInfo.Arguments = @"/c ""dir""";

        process.OutputDataReceived += ProcessOutputDataHandler;
        process.ErrorDataReceived += ProcessErrorDataHandler;

        process.Start();
        process.BeginOutputReadLine();
        process.BeginErrorReadLine();


        // Send a directory command and an exit command to the shell
        process.StandardInput.WriteLine("cd " + currentPath);
        process.StandardInput.WriteLine("ibt -mic");
    }

【问题讨论】:

  • 您尝试过什么,您有任何尝试过的代码吗?我们不会为您从头开始编写代码,但如果您向我们展示您目前所拥有的,您就更有可能得到回复。
  • 请查看编辑
  • 您发布的代码应该可以工作,它对我有用。你确定这是在执行吗?
  • @null:OP 并未尝试关闭发生Click 事件的程序。他们想关闭一个完全不同的程序。当然,Stack Overflow 上有很多问题和答案已经在讨论如何做到这一点(例如,将WM_CLOSE 发布到程序中,或调用Process.Kill()
  • 自从您上次编辑以来,可能发生的情况是您“关注”了 cmd 而不是实际的控制台,也许可以尝试关闭该进程或向其发送“退出”命令。

标签: c# .net winforms console


【解决方案1】:

在 Form 的 Dispose 函数中可以写:

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
        //Get the process Id
        var pr = Process.GetCurrentProcess();
        //Look for the parent ID
        using (var query = new ManagementObjectSearcher(
          "SELECT * " +
          "FROM Win32_Process " +
          "WHERE ProcessId=" + pr.Id))
        {
            var dadP = query
              .Get()
              .OfType<ManagementObject>()
              .Select(p => Process.GetProcessById((int)(uint)p["ParentProcessId"]))
              .FirstOrDefault();
            //Kill the parent
            dadP.CloseMainWindow();
        }            
    }

【讨论】:

    【解决方案2】:

    您可以使用 System.Diagnostics.Process 启动和停止另一个 exe。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多