-1

I have a c# program and currently I run 2 windows, the first one is form window and and the second one is console window for debugging.

The console window created accoeding the following:

Create a Windows Form project...

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

How can I close the console window from the form by button click?

Edit:

I tried to do the following but this is close the form window only..

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

Edit 2: the previous code works only before the following code performed.

        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");
    }
zak_p
  • 43
  • 1
  • 10
  • 1
    What have you tried, and do you have any code from an attempt at this? We won't write code from scratch for you, but if you show us what you have thus far, you're much more likely to get a response. – user2366842 Aug 07 '17 at 18:16
  • See edit please – zak_p Aug 07 '17 at 18:19
  • The code you posted should work, it works for me. Are you sure this is being executed? – Pau C Aug 07 '17 at 18:22
  • @null: the OP isn't trying to close the program in which the `Click` event occurs. They want to close a completely different program. Of course, there are many questions and answers on Stack Overflow already discussing how to do this (e.g. posting `WM_CLOSE` to the program, or calling `Process.Kill()`) – Peter Duniho Aug 07 '17 at 18:25
  • Since your last edit maybe what happens is that you have "focus" on the cmd and not the actual console, maybe try closing that process or sending the "exit" command to it. – Pau C Aug 07 '17 at 18:36

2 Answers2

0

You can use System.Diagnostics.Process to start and stop another exe.

James
  • 2,812
  • 3
  • 22
  • 33
0

In the Dispose function of the Form you can write:

    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();
        }            
    }
Moti Hamo
  • 178
  • 1
  • 5