在我的控制台应用程序的主体中,如果用户请求将其升级运行,我正在运行应用程序的升级版本。关闭控制台窗口启动新主版本时
的代码是这样做的
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