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

3

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.

Umberto answered 2/12, 2009 at 16:24 Comment(0)
F
3

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");
Farmhand answered 22/7, 2011 at 9:59 Comment(1)
Per #17847918, 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).Pablopabon
S
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.

Spadix answered 10/12, 2009 at 11:31 Comment(1)
Thanks, interesting. Does it mean that there is no way in .NET?Umberto
F
3

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");
Farmhand answered 22/7, 2011 at 9:59 Comment(1)
Per #17847918, 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).Pablopabon
P
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

Progressionist answered 12/10, 2015 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.