在我的控制檯應用程序的主體中,如果用戶請求將其升級運行,我正在運行應用程序的升級版本。關閉控制檯窗口啓動新主版本時
的代碼是這樣做的
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;
我不認爲你可以「關閉」它不會殺死整個應用程序,你發現了,但你可以使用AP /調用隱藏找到[這裏] (http://stackoverflow.com/questions/3563744/how-can-i-hide-a-console-window)。我的意思是,用這個人的問題中的代碼來得到你想要的。我會將「答案」與相同的代碼聯繫起來,但我沒有找到足夠快的答案。 – Quantic
如果註釋掉'process.WaitForExit()'後面的行,會發生什麼? – Kateract
@Quantic該代碼隱藏它,但並沒有關閉它,當你關閉提升的實例隱藏的控制檯然後開始運行時會發生什麼 – nlstack01