2015-10-05 118 views
2

这是一个有点复杂的问题。我尝试过所有可能的事情,但仍然没有工作。我运行CMD运行WinForm应用程序,然后在cmd上运行另一个应用程序(控制台应用程序)。它在/c START xyz上工作,但是当应用程序结束时CMD总是关闭。我想暂停这个窗口。如何在执行后保持控制台窗口打开?

ProcessStartInfo processInfo = new ProcessStartInfo { 
    FileName = "cmd.exe", 
    WorkingDirectory = Path.GetDirectoryName(YourApplicationPath), 
    Arguments = "/K START " + cmdparametr, 
    RedirectStandardOutput = true, 
    RedirectStandardInput = true, 
    RedirectStandardError = true, 
    CreateNoWindow = false, 
    UseShellExecute = false, 
    WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal, 
}; 

Process p = new Process { 
    StartInfo = processInfo 
}; 
p.Start(); 

int ExitCode; 
p.WaitForExit(); 

// *** Read the streams *** 
string output = p.StandardOutput.ReadToEnd(); 
string error = p.StandardError.ReadToEnd(); 

ExitCode = p.ExitCode; 

MessageBox.Show("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output)); 
MessageBox.Show("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error)); 
MessageBox.Show("ExitCode: " + ExitCode.ToString(), "ExecuteCommand"); 
p.Close(); 

ReadStream工作当我添加参数:START /b但我认为这并不重要。
WaitForExit()不起作用。

是否有可能通过命令暂停应用可能是这样的: /k start xyz.exe & PAUSE


我的应用程序是控制台应用程序!

+0

您是否尝试过将'&pause'作为您的问题记录添加到命令的末尾?也许相关 - 是否有任何理由重定向输入? –

回答

2

可以使用pause -command内C#如果你想: 我用它如下:

解决方案№1:

//optional: Console.WriteLine("Press any key ..."); 
Console.ReadLine(true); 

解决方案№2:(用途P/Invoke)

// somewhere in your class 
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, SetLastError=true)] 
public static extern int system(string command); 

public static int Main(string[] argv) 
{ 
    // your code 


    system("pause"); // will automaticly print the localized string and wait for any user key to be pressed 
    return 0; 
} 


编辑:您可以动态创建一个临时批处理文件,并执行它,例如:

string bat_path = "%temp%/temporary_file.bat"; 
string command = "command to be executed incl. arguments"; 

using (FileStream fs = new FileStream(bat_path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) 
using (StreamWriter sw = new StreamWriter(fs, Encoding.Default)) 
{ 
    sw.WriteLine("@echo off"); 
    sw.WriteLine(command); 
    sw.WriteLine("PAUSE"); 
} 

ProcessStartInfo psi = new ProcessStartInfo() { 
    WorkingDirectory = Path.GetDirectoryName(YourApplicationPath), 
    RedirectStandardOutput = true, 
    RedirectStandardInput = true, 
    RedirectStandardError = true, 
    CreateNoWindow = false, 
    UseShellExecute = false, 
    WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal 
}; 

Process p = new Process() { 
    StartInfo = psi; 
}; 
p.Start(); 

int ExitCode; 
p.WaitForExit(); 

// *** Read the streams *** 
string output = p.StandardOutput.ReadToEnd(); 
string error = p.StandardError.ReadToEnd(); 

ExitCode = p.ExitCode; 

MessageBox.Show("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output)); 
MessageBox.Show("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error)); 
MessageBox.Show("ExitCode: " + ExitCode.ToString(), "ExecuteCommand"); 
p.Close(); 

File.Delete(bat_path); 
+0

它是Windows窗体应用程序,而不是控制台应用程序。 – test

+0

@test:我编辑了我的答案 – Unknown6656

+0

谢谢!这就是我想要达到的目标。 – test

1

为了防止控制台应用程序的关闭,你可以使用:

Console.ReadLine(); 

它将等待任何键,也不会立即关闭。

+0

当任一应用程序没有控制台或控制台输入已从文件重定向时,无法读取密钥。尝试Console.Read。 – test

+0

@test yep。固定。 – Slashy

+0

不起作用...如果我仅使用'/ c START xyz',我会得到一个窗口,并在完成时关闭窗口。 – test

1

不包括START命令,使用这样的

processInfo.Arguments = "/K " + your_console_app_exe_path_and_args; 

确保在需要的地方附上双引号。

+0

没有,没有参数'START'我没有看到控制台应用程序的任何结果。 – test

+0

这是因为您正在重定向您的控制台应用程序的输出。 –

相关问题