英文:
How can I change the default console host application for Windows?
问题
I have set the default console host application to Windows Terminal using developers section in window settings. However, sometimes I want to run my console applications (currently using c# and dotnet 7) in other hosts, such as cmd.exe or PowerShell. How can I choose where my console applications are hosted on Windows in code? Is there a way to set different hosts for different applications?
I am using Windows 10 version 21H2 and Visual Studio 2022.
Thank you for your help.
英文:
I have set the default console host application to Windows Terminal using developers section in window settings. However, sometimes I want to run my console applications (currently using c# and dotnet 7) in other hosts, such as cmd.exe or PowerShell. How can I choose where my console applications are hosted on Windows in code? Is there a way to set different hosts for different applications?
I am using Windows 10 version 21H2 and Visual Studio 2022.
Thank you for your help.
答案1
得分: 1
I'm not aware of a per-application configuration mechanism, but you can invoke your program via the desired terminal application, conhost.exe
(for an old-style console window, via the console host) or wt.exe
(Windows Terminal).
Here's a PowerShell example using a cmd.exe
sample command (should be easy to adapt to C#):
using namespace System.Diagnostics
# Launch via conhost.exe (console host)
[Process]::Start(
[ProcessStartInfo] @{
FileName = "conhost.exe"
Arguments = "cmd.exe /k 'echo Hi there.'"
}
)
# Launch via wt.exe (Windows Terminal)
[Process]::Start(
[ProcessStartInfo] @{
FileName = "wt.exe"
Arguments = "cmd.exe /k 'echo Hi there.'"
}
)
Caveat re trying to capture a launched program's output programmatically, via .RedirectStandardOut
and .RedirectStandardError
with the above technique:
-
With Windows Terminal, that is fundamentally unsupported.
-
With
conhost.exe
, you technically can, but you'll get a preamble with VT escape sequences (expressed as an expandable PowerShell (Core) string):"␛[?25l␛[2J␛[m␛[H␛]0;C:\Windows\SYSTEM32\conhost.exe␛[?25h"
英文:
I'm not aware of a per-application configuration mechanism, but you can invoke your program via the desired terminal application, conhost.exe
(for an old-style console window, via the console host) or wt.exe
(Windows Terminal).
Here's a PowerShell example using a cmd.exe
sample command (should be easy to adapt to C#):
using namespace System.Diagnostics
# Launch via conhost.exe (console host)
[Process]::Start(
[ProcessStartInfo] @{
FileName = "conhost.exe"
Arguments = "cmd.exe /k `"echo Hi there.`""
}
)
# Launch via wt.exe (Windows Terminal)
[Process]::Start(
[ProcessStartInfo] @{
FileName = "wt.exe"
Arguments = "cmd.exe /k `"echo Hi there.`""
}
)
Caveat re trying to capture a launched program's output programmatically, via .RedirectStandardOut
and .RedirectStandardError
with the above technique:
-
With Windows Terminal, that is fundamentally unsupported.
-
With
conhost.exe
, you technically can, but you'll get a preamble with VT escape sequences (expressed as an expandable PowerShell (Core) string):"`e[?25l`e[2J`e[m`e[H`e]0;C:\Windows\SYSTEM32\conhost.exe`a`e[?25h"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论