7

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.

mark
  • 59,016
  • 79
  • 296
  • 580

3 Answers3

3

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.

Mattias S
  • 4,748
  • 2
  • 17
  • 18
3

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

JustFer
  • 31
  • 1
2

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");
Ray
  • 45,695
  • 27
  • 126
  • 169
  • Per https://stackoverflow.com/questions/17847418/how-do-i-change-the-window-title-after-starting-something-with-process-start, it looks like a Thread.Sleep() is needed - on my machine, a sleep of 1000ms does the trick. Not sure how to make it more robust (i.e. not rely on an arbitrary delay). – David Airapetyan May 11 '20 at 18:16