I am currently using TopShelf with a Console Application to create a Windows Service. When I run the code as a console application I use a few Console.WriteLine() to output
Also want to display a help depending on command line.
My solution is to open a new console.
internal static class NativeMethods
{
[DllImport("user32.dll")]
internal static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
internal static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("kernel32.dll")]
internal static extern bool AllocConsole();
[DllImport("kernel32.dll")]
internal static extern IntPtr GetConsoleWindow();
}
public static void ShowWindow(IntPtr hWnd, ShowWindowCommand cmd = ShowWindowCommand.Restore)
{
NativeMethods.SetForegroundWindow(hWnd);
NativeMethods.ShowWindowAsync(hWnd, (int)cmd);
}
public static void ShowConsoleWindow()
{
var handle = NativeMethods.GetConsoleWindow();
if (handle == IntPtr.Zero)
NativeMethods.AllocConsole();
else
ShowWindow(handle, ShowWindowCommand.Show);
}
ShowWindowCommand is just an enum built from here
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow