2016-04-25 80 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