How do I create a C# app that decides itself whether to show as a console or windowed app?

前端 未结 11 791
广开言路
广开言路 2020-11-28 21:10

Is there a way to launch a C# application with the following features?

  1. It determines by command-line parameters whether it is a windowed or console app
11条回答
  •  失恋的感觉
    2020-11-28 21:32

    As far as I am aware there is a flag in the exe that tells it whether to run as console or windowed app. You can flick the flag with tools that come with Visual Studio, but you cann't do this at runtime.

    If the exe is compiled as a console, then it will always open a new console if its not started from one. If the the exe is an application then it can't output to the console. You can spawn a separate console - but it won't behave like a console app.

    I the past we have used 2 separate exe's. The console one being a thin wrapper over the forms one (you can reference an exe as you would reference a dll, and you can use the [assembly:InternalsVisibleTo("cs_friend_assemblies_2")] attribute to trust the console one, so you don't have to expose more than you need to).

提交回复
热议问题