How to run a C# console application with the console hidden
If you are using the ProcessStartInfo
class you can set the window style to hidden - in the case of console (not GUI) applications, you have to set CreateNoWindow to 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
Show/Hide the console window of a C# console application
Here’s how:
using System.Runtime.InteropServices;
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0;
const int SW_SHOW = 5;
var handle = GetConsoleWindow();
// Hide
ShowWindow(handle, SW_HIDE);
// Show
ShowWindow(handle, SW_SHOW);
Hide the console window from a console application
Go to the Application Properties and change Output Type from Console Application to Windows Application.
Or you can do it using a code below
using System.Runtime.InteropServices;
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
And in main
const int SW_HIDE = 0;
const int SW_SHOW = 5;
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE); // To hide
ShowWindow(handle, SW_SHOW); // To show
Also, you can run you application as a service. In order to do this you should create a service - File->New Project->Visual C#->Windows->Windows Service. Then create a public method StartWork()
and add all you logic there. And call this method in OnStart()
.
protected override void OnStart(string[] args)
{
try
{
this.StartJobs();
}
catch (Exception ex)
{
// catching exception
}
}
public void StartWork()
{
// all the logic here
}
In main you should create this service and use System.ServiceProcess.ServiceBase.Run()
to run it as service or call StartWork()
to run it as console application.
static void Main(string[] args)
{
TestService = new TestService ();
#if DEBUG
TestService.StartWork()();
#else
System.ServiceProcess.ServiceBase.Run(TestService );
#endif
}
Using Invoke to run a hidden console application
I can think of two solutions:
1.Change your console application output type from Console Application
to Windows Application
. You can change it from Project Properties
. This will hide the console window.
2.Write your assembly bytes to a file and use Process.Start to hide the console window. Something like this:
var f = Path.GetTempFileName() + ".exe";
File.WriteAllBytes(f, assemblyBytes);
var p = new Process();
p.StartInfo.FileName = f;
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
p.Start();
Hide Console Window in C# Console Application
Change the output type from Console Application to Windows Application. This can be done under Project -> Properties -> Application in Visual Studio:
How to hide a console application in C#
Compile it as a Windows Forms application. Then it won't display any UI, if you do not explicitly open any Windows.
how can i publish a C# .net console app without the console?
An easy way to do this is to change <OutputType>Exe</OutputType>
to <OutputType>WinExe</OutputType>
in your csproj. Once you do that, your application won't open a console window.
Later on, if you want to open a console window for debugging or whatever you can PInvoke AllocConsole().
Related Topics
Turn a Simple C# Dll into a Com Interop Component
Conversion of System.Array to List
Windows Application Startup Error Exception Code: 0Xe0434352
Benchmarking Small Code Samples in C#, Can This Implementation Be Improved
Handle Modelstate Validation in ASP.NET Web API
How to Read an Entire File to a String Using C#
Is Ruby's Code Block Same as C#'s Lambda Expression
Does C++11 Have C#-Style Properties
Sharing Memory Between Two Applications
How to Configure the Web.Config to Allow Requests of Any Length
Is Shifting Bits Faster Than Multiplying and Dividing in Java? .Net
How to Get Rendered HTML (Processed by JavaScript) in Webbrowser Control
Adding Your Own HTMLhelper in ASP.NET MVC 3
Evaluate String with Math Operators