Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one specify the window title for a console application started with System.Diagnostics.Process.Start()?

I am starting a new instance of a console application from my .NET code using the Process.Start() method. I was wondering if I can specify the title of the console window hosting the spawned process. Could not find anything suitable in ProcessStartInfo.

As a last resort I can P/Invoke to talk to Win32 API directly, but I'd rather not.

Any ideas?

Thanks.

like image 685
mark Avatar asked Dec 02 '09 16:12

mark


3 Answers

The easiest way I can think of is to create a batch file that sets the title (using the title command) and then executes the application. Then Start the .bat file instead.

like image 89
Mattias S Avatar answered Oct 16 '22 13:10

Mattias S


Inside the e.g. script of powershell I do use:

# Set the Window Title as a reference
[System.Console]::Title = "Main title of the window"

Got it from here, maybe useful: http://blogs.msdn.com/b/rob/archive/2012/08/21/setting-the-title-of-the-command-prompt-window.aspx

like image 35
FGC Avatar answered Oct 16 '22 13:10

FGC


I know it sounds like you know the P/Invoke way of doing this, but for anyone else this is how you do it

[DllImport("User32.dll")]
public static extern bool SetWindowText(IntPtr hwnd, string title);


SetWindowText(myProcess.MainWindowHandle, "my new title");
like image 2
Ray Avatar answered Oct 16 '22 14:10

Ray