2016-04-25 119 views
0

在我的控制檯應用程序的主體中,如果用戶請求將其升級運行,我正在運行應用程序的升級版本。關閉控制檯窗口啓動新主版本時

的代碼是這樣做的

elevated.exe myexe.exe //a /elevated 

此代碼是在主正在運行,因此什麼時候myexe是跑它會打開一個控制檯窗口中點擊下面的代碼並創建新的實例另一個控制檯窗口發生。

如何以編程方式關閉初始窗口而不關閉新窗口?

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

進入這裏

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

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

下面的代碼是RunElevated代碼非常標準的肉..

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; 
+1

我不認爲你可以「關閉」它不會殺死整個應用程序,你發現了,但你可以使用AP /調用隱藏找到[這裏] (http://stackoverflow.com/questions/3563744/how-can-i-hide-a-console-window)。我的意思是,用這個人的問題中的代碼來得到你想要的。我會將「答案」與相同的代碼聯繫起來,但我沒有找到足夠快的答案。 – Quantic

+0

如果註釋掉'process.WaitForExit()'後面的行,會發生什麼? – Kateract

+0

@Quantic該代碼隱藏它,但並沒有關閉它,當你關閉提升的實例隱藏的控制檯然後開始運行時會發生什麼 – nlstack01

回答

1

OK,也許我現在知道發生了什麼事情。當您使用UseShellExecute = false時,程序將在相同的命令窗口中運行,您將通過Environment.Exit(0)關閉該命令窗口。

所以改成這樣:

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(); 

難道不重定向輸出,因爲1)你不能用UseShellExecute=true,2)你反正關閉主應用程序,爲什麼重定向的事情是退出應用程序幾毫秒。

通過這些更改,您可以在自己的隱藏窗口中生成應用程序,然後只需Environment.Exit(0)您的主應用程序將殺死未提升的應用程序,但不會觸及您生成的進程。

這裏是一個完全工作示例:

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(); 
    } 
} 
} 
+0

我打電話後調用Enviroment.Exit(0)來產生提升的應用程序,但它沒有效果。我能以某種方式將spawn的輸出傳送到主控制檯嗎? – nlstack01

+0

也手動關閉控制檯窗口無論何種原因重新運行代碼 – nlstack01

+0

我昨天測試了代碼,它似乎工作。你必須擺脫所有的過程,並將其替換爲我的。例如,你不能*調用'process.WaitForExit();',否則你的程序永遠不會調用'Environment.Exit(0)',因爲它正在等待新生成的進程首先退出。具有完整的工作程序。 – Quantic