0

In main of my console application I am running an elevated version of my application if the user requests to run it elevated.

the code is doing this

elevated.exe myexe.exe //a /elevated

This code is being run in main so what happens when myexe is ran it opens a console window hits the code below and creates another console window with the new instance.

How do I close the initial window programmatically without closing the new one?

Environment.Exit(0) //closes the entire application THIS WONT WORK

enter code here

 public void Run(string[] args) //myexe.exe
        {

        if (args[0] == "/elevated")
        {
            _command.RunElevated(path, arguments); 
            return;
        }
    }

Here is the meat of the RunElevated code pretty standard..

var process = new Process();
        if (workingDirectory != null) process.StartInfo.WorkingDirectory = workingDirectory;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.FileName = fileName;
        process.StartInfo.Arguments = arguments;
        process.Start();

        // deal with output
        standardOutput = process.StandardOutput.ReadToEnd();
        standardError = process.StandardError.ReadToEnd();

        // wait til the process exits
        process.WaitForExit();

        int exitCode = process.ExitCode;
nlstack01
  • 789
  • 2
  • 7
  • 30
  • 1
    I don't think you can "close" it without killing the whole app as you found out, but you can hide it using a p/invoke found [here](http://stackoverflow.com/questions/3563744/how-can-i-hide-a-console-window). And I mean, use the code in the guy's question to get what you want. I would have linked an "answer" with the same code but I didn't find one fast enough. – Quantic Apr 25 '16 at 19:40
  • What happens if you comment out the `process.WaitForExit()` and the line after it? – Kateract Apr 25 '16 at 19:40
  • @Quantic that code hides it but doesnt close it and what happens when you close the elevated instance the hidden console then starts to run – nlstack01 Apr 25 '16 at 21:10

1 Answers1

1

OK maybe I know what's going on now. When you use UseShellExecute = false, then the program runs in the same command window, which you are closing with Environment.Exit(0).

So change to this:

   var process = new Process();
    if (workingDirectory != null) process.StartInfo.WorkingDirectory = workingDirectory;
    process.StartInfo.UseShellExecute = true;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.FileName = fileName;
    process.StartInfo.Arguments = arguments;
    process.Start();

Do no redirect output because 1) you can't with UseShellExecute=true, and 2) you are closing your main application anyway so why redirect things to an app that is exiting in a couple milliseconds.

With these changes you spawn your app in its own, hidden, window, then just Environment.Exit(0) your main application which will kill the non-elevated one but won't touch the process you spawned.

Here's an entirely working example:

using System;
using System.Diagnostics;

namespace ConsoleApplication4
{
class Program
{
    static void Main(string[] args)
    {
        if (args.Length > 0 && args[0] == "/elevated")
        {
            var process = new Process();
            /*process.StartInfo.WorkingDirectory = */
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.CreateNoWindow = false;
            process.StartInfo.FileName = "ConsoleApplication4.exe";
            process.StartInfo.Arguments = "startedThroughElevatedCodePath";
            process.Start();
            Environment.Exit(0);
        }

        if (args.Length > 0 && args[0] == "startedThroughElevatedCodePath")
        {
            Console.WriteLine("Hello from elevated");
        }
        else
        {
            Console.WriteLine("Hello from not elevated");
        }

        Console.Read();
    }
}
}
Quantic
  • 1,779
  • 19
  • 30
  • I call Enviroment.Exit(0) after my call to spawn the elevated app but it has no effect. Can I somehow just pipe the output of the spawn into the main console? – nlstack01 Apr 26 '16 at 19:50
  • Also manually closing the console windows reruns the code once for whatever reason – nlstack01 Apr 26 '16 at 19:51
  • I tested the code yesterday and it seemed working. You have to get rid of all your process stuff and replace it with mine. You *cannot* call `process.WaitForExit();`, for example, or else your program never makes it to `Environment.Exit(0)` because it's waiting for the newly spawned process to exit first.I just edited the post with a complete working program. – Quantic Apr 26 '16 at 20:02