How can I bring a console application window to front in C# (especially when running the Visual Studio debugger)?
Asked
Active
Viewed 1.8k times
14
-
In what context and why do you want to do this? Whilst there are legitimate cases, usually this sort of thing is more trouble than it's worth. – ICR Oct 17 '08 at 19:24
3 Answers
19
This is what I would do.
[DllImport("kernel32.dll", ExactSpelling = true)]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public void BringConsoleToFront()
{
SetForegroundWindow(GetConsoleWindow());
}

ryanb9
- 211
- 3
- 4
-
beautiful - except i needed to add `[DllImport("user32.dll")]` `[return: MarshalAs(UnmanagedType.Bool)]` `public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);` and `ShowWindow(hWnd, 9); //restore` – AndrewFreese Dec 02 '21 at 15:00
19
It's hacky, it's horrible, but it works for me (thanks, pinvoke.net!):
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
public class Test
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint="FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr zeroOnly, string lpWindowName);
public static void Main()
{
string originalTitle = Console.Title;
string uniqueTitle = Guid.NewGuid().ToString();
Console.Title = uniqueTitle;
Thread.Sleep(50);
IntPtr handle = FindWindowByCaption(IntPtr.Zero, uniqueTitle);
if (handle == IntPtr.Zero)
{
Console.WriteLine("Oops, cant find main window.");
return;
}
Console.Title = originalTitle;
while (true)
{
Thread.Sleep(3000);
Console.WriteLine(SetForegroundWindow(handle));
}
}
}

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194
-
Please don't ever add this to your code just to get it to appear on top when debugging. – tvanfosson Feb 05 '09 at 14:31
-
4@tv - Maybe you could give an explanation to your absolute statement. Personally I think this is a legitimate and recommended use of window management functions. It's not the best DRY approach but with a little refactoring it's a common practice in win32 application development. It's not hackey, it's how you move windows around the desktop. – Marcus Pope Jul 08 '10 at 16:15
-
just to add, if you were looking to run this code only when in the visual studio debugger you can use the following: if (Debugger.IsAttached == true) { ... } – Marcus Pope Jul 08 '10 at 16:32
-
9
-
3@JonSkeet .. or not. As the eyes age, seeing things like (!MyCondition) becomes easier to miss and create bugs. – StingyJack Jun 11 '17 at 12:10
-
@StingyJack: I think we'll have to agree to disagree about that. In particular, it's clearer with a monospace font than as you've written it there. – Jon Skeet Jun 11 '17 at 12:28
-4
Get two monitors (at least) and open VisualStudio in the secondary monitor. When you run your app from within VisualStudio it will start up by default on the primary monitor. Since it's the last app to be opened, it starts on top and changing over to VisualStudio doesn't affect it. Works for me anyway.
If you don't already have a second monitor, IMHO, you should.

tvanfosson
- 524,688
- 99
- 697
- 795