有没有一种方法可以在执行控制台应用程序时隐藏控制台窗口?
我目前正在使用 Windows Forms 应用程序启动控制台进程,但我不希望在任务运行时显示控制台窗口。
有没有一种方法可以在执行控制台应用程序时隐藏控制台窗口?
我目前正在使用 Windows Forms 应用程序启动控制台进程,但我不希望在任务运行时显示控制台窗口。
如果您编写了控制台应用程序,则可以默认将其隐藏。
创建一个新的控制台应用程序,然后将“输出类型”更改为“Windows应用程序”(在项目属性中完成)。
ProcessStartInfo
类,则可以将窗口样式设置为隐藏 - 对于控制台(非GUI)应用程序,您必须将CreateNoWindow设置为true
。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
yourprocess.StartInfo.UseShellExecute = false;
yourprocess.StartInfo.CreateNoWindow = true;
在 yourprocess.start();
之前,进程将被隐藏
Process.GetProcessById(pid).WaitForExit(...)
来等待或轮询。只有一个选项。 - undefined简单的回答是:进入您的控制台应用程序属性(项目属性)。在“应用程序”选项卡中,只需将“输出类型”更改为“Windows应用程序”。就这样。
你可以使用FreeConsole API来将控制台从进程中分离:
[DllImport("kernel32.dll")]
static extern bool FreeConsole();
(当然,前提是您可以访问控制台应用程序的源代码)
FreeConsole
做了它的名字所说的,但它不能防止Windows在调用它之前显示控制台。 - Jader Dias[DllImport("kernel32.dll")] static extern bool AllocConsole();
允许您从标准的Win32应用程序中轻松地运行并搭配使用控制台。 - seheprivate 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();
}
如果您正在创建一个不需要用户输入的程序,您可以将其创建为服务。服务不会显示任何类型的用户界面。
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
const int SW_HIDE = 0;
const int SW_SHOW = 5;
如果您想隐藏它,请使用以下命令:
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
如果你想显示控制台:
var handle = GetConsoleWindow();
ShowWindow(handle, SW_SHOW);
using System;
using System.Runtime.InteropServices;
namespace WhateverNamepaceYouAreUsing
{
class Magician
{
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int HIDE = 0;
const int SHOW = 5;
public static void DisappearConsole()
{
ShowWindow(GetConsoleWindow(), HIDE);
}
}
}
Magician.DisappearConsole();
。根据Adam Markowitz上面的回答,以下方法适用于我:
process = new Process();
process.StartInfo = new ProcessStartInfo("cmd.exe", "/k \"" + CmdFilePath + "\"");
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//process.StartInfo.UseShellExecute = false;
//process.StartInfo.CreateNoWindow = true;
process.Start();