Is there a way to hide the console window when executing a console application?
有没有办法在执行控制台应用程序时隐藏控制台窗口?
I am currently using a Windows Forms application to start a console process, but I don't want the console window to be displayed while the task is running.
我目前正在使用Windows窗体应用程序来启动控制台进程,但我不希望在任务运行时显示控制台窗口。
13 个解决方案
#1
If you are using the ProcessStartInfo
class you can set the window style to hidden:
如果您使用的是ProcessStartInfo类,则可以将窗口样式设置为隐藏:
System.Diagnostics.ProcessStartInfo start =
new System.Diagnostics.ProcessStartInfo();
start.FileName = dir + @"\Myprocesstostart.exe";
start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
#2
If you wrote the console application you can make it hidden by default.
如果您编写了控制台应用程序,则可以将其隐藏起来。
Create a new console app then then change the "Output Type" type to "Windows Application" (done in the project properties)
创建一个新的控制台应用程序,然后将“输出类型”类型更改为“Windows应用程序”(在项目属性中完成)
#3
If you are using Process Class then you can write
如果您正在使用Process Class,那么您可以编写
yourprocess.StartInfo.UseShellExecute = false;
yourprocess.StartInfo.CreateNoWindow = true;
before yourprocess.start();
and process will be hidden
在yourprocess.start()之前;和过程将被隐藏
#4
Simple answer is that: Go to your console app's properties(project's properties).In the "Application" tab, just change the "Output type" to "Windows Application". That's all.
简单的答案是:转到控制台应用程序的属性(项目属性)。在“应用程序”选项卡中,只需将“输出类型”更改为“Windows应用程序”。就这样。
#5
You can use the FreeConsole API to detach the console from the process :
您可以使用FreeConsole API将控制台与进程分离:
[DllImport("kernel32.dll")]
static extern bool FreeConsole();
(of course this is applicable only if you have access to the console application's source code)
(当然,只有在您可以访问控制台应用程序的源代码时才适用)
#6
If you're interested in the output, you can use this function:
如果您对输出感兴趣,可以使用此功能:
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();
}
It runs the given command line program, waits for it to finish and returns the output as string.
它运行给定的命令行程序,等待它完成并将输出作为字符串返回。
#7
If you're creating a program that doesn't require user input you could always just create it as a service. A service won't show any kind of UI.
如果您正在创建一个不需要用户输入的程序,您可以随时将其创建为服务。服务不会显示任何类型的UI。
#8
I know I'm not answering exactly what you want, but I am wondering if you're asking the right question.
我知道我并没有回答你想要的,但我想知道你是否提出了正确的问题。
Why don't you use either:
你为什么不使用:
- windows service
- create a new thread and run your process on that
创建一个新线程并在其上运行您的进程
Those sound like better options if all you want is to run a process.
如果你想要的只是运行一个过程,那些听起来就像是更好的选择。
#9
Add this to your class to import the DLL file:
将其添加到您的类以导入DLL文件:
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0;
const int SW_SHOW = 5;
And then if you want to hide it use this command:
然后,如果要隐藏它,请使用以下命令:
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
And if you want to show the console:
如果你想显示控制台:
var handle = GetConsoleWindow();
ShowWindow(handle, SW_SHOW);
#10
Based on Adam Markowitz's answer above, following worked for me:
根据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();
#11
I've got a general solution to share:
我有一个通用的解决方案:
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);
}
}
}
Just include this class in your project, and call Magician.DisappearConsole();
.
只需在项目中包含此类,然后调用Magician.DisappearConsole();.
A console will flash when you start the program by clicking on it. When executing from the command prompt, the command prompt disappears very shortly after execution.
通过单击启动程序时,控制台将闪烁。从命令提示符执行时,命令提示符在执行后很快就会消失。
I do this for a Discord Bot that runs forever in the background of my computer as an invisible process. It was easier than getting TopShelf to work for me. A couple TopShelf tutorials failed me before I wrote this with some help from code I found elsewhere. ;P
我这样做是为了一个在我的计算机背景中永远运行的Discord Bot作为一个看不见的过程。这比让TopShelf为我工作更容易。在我用其他地方找到的代码帮助我写这篇文章之前,一些TopShelf教程失败了。 ,P
I also tried simply changing the settings in Visual Studio > Project > Properties > Application to launch as a Windows Application instead of a Console Application, and something about my project prevented this from hiding my console - perhaps because DSharpPlus demands to launch a console on startup. I don't know. Whatever the reason, this class allows me to easily kill the console after it pops up.
我还尝试简单地更改Visual Studio>项目>属性>应用程序中的设置以作为Windows应用程序而不是控制台应用程序启动,而我的项目的某些内容阻止了这隐藏我的控制台 - 可能是因为DSharpPlus要求在启动时启动控制台。我不知道。不管是什么原因,这个类允许我在弹出后轻松杀死控制台。
Hope this Magician helps somebody. ;)
希望这位魔术师帮助某人。 ;)
#12
Just write
ProcessStartInfo psi= new ProcessStartInfo("cmd.exe");
......
psi.CreateNoWindow = true;
#13
Although as other answers here have said you can change the "Output type" to "Windows Application", please be aware that this will mean that you cannot use Console.In
as it will become a NullStreamReader.
虽然这里的其他答案已经说过您可以将“输出类型”更改为“Windows应用程序”,但请注意,这将意味着您无法使用Console.In,因为它将成为NullStreamReader。
Console.Out
and Console.Error
seem to still work fine however.
然而,Console.Out和Console.Error似乎仍然正常工作。
#1
If you are using the ProcessStartInfo
class you can set the window style to hidden:
如果您使用的是ProcessStartInfo类,则可以将窗口样式设置为隐藏:
System.Diagnostics.ProcessStartInfo start =
new System.Diagnostics.ProcessStartInfo();
start.FileName = dir + @"\Myprocesstostart.exe";
start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
#2
If you wrote the console application you can make it hidden by default.
如果您编写了控制台应用程序,则可以将其隐藏起来。
Create a new console app then then change the "Output Type" type to "Windows Application" (done in the project properties)
创建一个新的控制台应用程序,然后将“输出类型”类型更改为“Windows应用程序”(在项目属性中完成)
#3
If you are using Process Class then you can write
如果您正在使用Process Class,那么您可以编写
yourprocess.StartInfo.UseShellExecute = false;
yourprocess.StartInfo.CreateNoWindow = true;
before yourprocess.start();
and process will be hidden
在yourprocess.start()之前;和过程将被隐藏
#4
Simple answer is that: Go to your console app's properties(project's properties).In the "Application" tab, just change the "Output type" to "Windows Application". That's all.
简单的答案是:转到控制台应用程序的属性(项目属性)。在“应用程序”选项卡中,只需将“输出类型”更改为“Windows应用程序”。就这样。
#5
You can use the FreeConsole API to detach the console from the process :
您可以使用FreeConsole API将控制台与进程分离:
[DllImport("kernel32.dll")]
static extern bool FreeConsole();
(of course this is applicable only if you have access to the console application's source code)
(当然,只有在您可以访问控制台应用程序的源代码时才适用)
#6
If you're interested in the output, you can use this function:
如果您对输出感兴趣,可以使用此功能:
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();
}
It runs the given command line program, waits for it to finish and returns the output as string.
它运行给定的命令行程序,等待它完成并将输出作为字符串返回。
#7
If you're creating a program that doesn't require user input you could always just create it as a service. A service won't show any kind of UI.
如果您正在创建一个不需要用户输入的程序,您可以随时将其创建为服务。服务不会显示任何类型的UI。
#8
I know I'm not answering exactly what you want, but I am wondering if you're asking the right question.
我知道我并没有回答你想要的,但我想知道你是否提出了正确的问题。
Why don't you use either:
你为什么不使用:
- windows service
- create a new thread and run your process on that
创建一个新线程并在其上运行您的进程
Those sound like better options if all you want is to run a process.
如果你想要的只是运行一个过程,那些听起来就像是更好的选择。
#9
Add this to your class to import the DLL file:
将其添加到您的类以导入DLL文件:
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0;
const int SW_SHOW = 5;
And then if you want to hide it use this command:
然后,如果要隐藏它,请使用以下命令:
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
And if you want to show the console:
如果你想显示控制台:
var handle = GetConsoleWindow();
ShowWindow(handle, SW_SHOW);
#10
Based on Adam Markowitz's answer above, following worked for me:
根据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();
#11
I've got a general solution to share:
我有一个通用的解决方案:
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);
}
}
}
Just include this class in your project, and call Magician.DisappearConsole();
.
只需在项目中包含此类,然后调用Magician.DisappearConsole();.
A console will flash when you start the program by clicking on it. When executing from the command prompt, the command prompt disappears very shortly after execution.
通过单击启动程序时,控制台将闪烁。从命令提示符执行时,命令提示符在执行后很快就会消失。
I do this for a Discord Bot that runs forever in the background of my computer as an invisible process. It was easier than getting TopShelf to work for me. A couple TopShelf tutorials failed me before I wrote this with some help from code I found elsewhere. ;P
我这样做是为了一个在我的计算机背景中永远运行的Discord Bot作为一个看不见的过程。这比让TopShelf为我工作更容易。在我用其他地方找到的代码帮助我写这篇文章之前,一些TopShelf教程失败了。 ,P
I also tried simply changing the settings in Visual Studio > Project > Properties > Application to launch as a Windows Application instead of a Console Application, and something about my project prevented this from hiding my console - perhaps because DSharpPlus demands to launch a console on startup. I don't know. Whatever the reason, this class allows me to easily kill the console after it pops up.
我还尝试简单地更改Visual Studio>项目>属性>应用程序中的设置以作为Windows应用程序而不是控制台应用程序启动,而我的项目的某些内容阻止了这隐藏我的控制台 - 可能是因为DSharpPlus要求在启动时启动控制台。我不知道。不管是什么原因,这个类允许我在弹出后轻松杀死控制台。
Hope this Magician helps somebody. ;)
希望这位魔术师帮助某人。 ;)
#12
Just write
ProcessStartInfo psi= new ProcessStartInfo("cmd.exe");
......
psi.CreateNoWindow = true;
#13
Although as other answers here have said you can change the "Output type" to "Windows Application", please be aware that this will mean that you cannot use Console.In
as it will become a NullStreamReader.
虽然这里的其他答案已经说过您可以将“输出类型”更改为“Windows应用程序”,但请注意,这将意味着您无法使用Console.In,因为它将成为NullStreamReader。
Console.Out
and Console.Error
seem to still work fine however.
然而,Console.Out和Console.Error似乎仍然正常工作。