簡體   English   中英

執行后如何保持控制台窗口打開?

[英]How to keep Console Window open after execution?

這是一個有點復雜的問題。 我可能已經嘗試了一切,但仍然無法正常工作。 我從運行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();

當我添加參數START /b時, ReadStream可以工作,但是我認為這並不重要。
WaitForExit()不起作用。

是否可以通過以下命令暫停應用程序: /k start xyz.exe & PAUSE


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

如果需要,可以在C#中使用pause -command:我按如下方式使用它:

解決方案№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); 

為了防止關閉控制台應用程序,您可以使用:

Console.ReadLine();

它將等待任何鍵,並且不會立即關閉。

不包括START命令,使用類似這樣的東西

processInfo.Arguments = "/K " + your_console_app_exe_path_and_args;

確保在需要時用雙引號引起來。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM