你如何更改 Windows 的默认控制台主机应用程序?

huangapple go评论94阅读模式
英文:

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"

huangapple
  • 本文由 发表于 2023年3月4日 03:11:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定