I need to bring the console from an C# form/console application to the front. I got the following code which will bring the console window to front from: bring a console window to front in c#
However why does this code example also prints true and how can I disable it?
public static void ToFront()
{
string originalTitle = Console.Title;
string uniqueTitle = Guid.NewGuid().ToString();
Console.Title = uniqueTitle;
Thread.Sleep(50);
IntPtr handle = FindWindowByCaption(IntPtr.Zero, uniqueTitle);
Console.Title = originalTitle;
Console.WriteLine(SetForegroundWindow(handle));
}
Change your last line from
Console.WriteLine(SetForegroundWindow(handle));
to
SetForegroundWindow(handle);
What you had was executing the function and printing the resulting value.
This line is the culprit:
Console.WriteLine(SetForegroundWindow(handle));
SetForeGroundWindow returns a bool, which is cast to a string automatically by WriteLine, and printed out.
Replace it with:
SetForegroundWindow(handle);
You are printing out the return of SetForgroundWindow which returns true when it succeeds. Just remove the class to console.writeline
Console.Title = originalTitle;
SetForegroundWindow(handle);
You can use;
SetForegroundWindow(handle);
not
Console.WriteLine(SetForegroundWindow(handle));
Given that the title is unique within the desktop, add a reference to Microsoft.VisualBasic, then:
using Microsoft.VisualBasic;
....
Interaction.AppActivate(Console.Title);
Edit: Sorry I didn't completely read the question, but this is a simple way of bring an app to the foreground if the VB import doesn't offend you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.