2015-10-05 130 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

這是因爲您正在重定向您的控制檯應用程序的輸出。 –

相關問題