开发者

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

开发者 https://www.devze.com 2022-12-13 13:39 出处:网络
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.

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 no开发者_如何学Ct.

Any ideas?

Thanks.


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.


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


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");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号