Aar*_*mas 135 c# console-application
有没有办法在执行控制台应用程序时隐藏控制台窗口?
我目前正在使用Windows窗体应用程序来启动控制台进程,但我不希望在任务运行时显示控制台窗口.
Sim*_*mon 190
如果您编写了控制台应用程序,则可以将其隐藏起来.
创建一个新的控制台应用程序,然后将"输出类型"类型更改为"Windows应用程序"(在项目属性中完成)
Ada*_*itz 151
如果您正在使用ProcessStartInfo该类,则可以将窗口样式设置为隐藏:
System.Diagnostics.ProcessStartInfo start =
new System.Diagnostics.ProcessStartInfo();
start.FileName = dir + @"\Myprocesstostart.exe";
start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Hides GUI
start.CreateNoWindow = true; //Hides console
Run Code Online (Sandbox Code Playgroud)
小智 67
如果您正在使用Process Class,那么您可以编写
yourprocess.StartInfo.UseShellExecute = false;
yourprocess.StartInfo.CreateNoWindow = true;
Run Code Online (Sandbox Code Playgroud)
之前yourprocess.start();和过程将被隐藏
小智 41
简单的答案是:转到控制台应用程序的属性(项目属性).在"应用程序"选项卡中,只需将"输出类型"更改为"Windows应用程序".就这样.
Tho*_*que 20
您可以使用FreeConsole API将控制台从进程中分离出来:
[DllImport("kernel32.dll")]
static extern bool FreeConsole();
Run Code Online (Sandbox Code Playgroud)
(当然,只有在您可以访问控制台应用程序的源代码时才适用)
如果您对输出感兴趣,可以使用此功能:
private static string ExecCommand(string filename, string arguments)
{
Process process = new Process();
ProcessStartInfo psi = new ProcessStartInfo(filename);
psi.Arguments = arguments;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
process.StartInfo = psi;
StringBuilder output = new StringBuilder();
process.OutputDataReceived += (sender, e) => { output.AppendLine(e.Data); };
process.ErrorDataReceived += (sender, e) => { output.AppendLine(e.Data); };
// run the process
process.Start();
// start reading output to events
process.BeginOutputReadLine();
process.BeginErrorReadLine();
// wait for process to exit
process.WaitForExit();
if (process.ExitCode != 0)
throw new Exception("Command " + psi.FileName + " returned exit code " + process.ExitCode);
return output.ToString();
}
Run Code Online (Sandbox Code Playgroud)
它运行给定的命令行程序,等待它完成并将输出作为字符串返回.
| 归档时间: |
|
| 查看次数: |
160962 次 |
| 最近记录: |