Set application output type programmatically

前端 未结 2 829
逝去的感伤
逝去的感伤 2020-12-21 08:40

I am programming an application using the command line application output type to display debug information in the console while MOGRE is handling the actual window creation

相关标签:
2条回答
  • 2020-12-21 08:42

    Not very sure, if there is pure .NET way to achieve what you want to do, but there is a way to achieve this by using Windows APIs:

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool AllocConsole();
    
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool FreeConsole();
    
    [DllImport("kernel32", SetLastError = true)]
    static extern bool AttachConsole(int dwProcessId);
    

    Here is code sample that could be helpful to you: Attach Console to Windows Forms application

    0 讨论(0)
  • 2020-12-21 08:59

    You can achieve this if you edit the .csproj manually:

    • Right click on the project node in Solution Explorer
    • Select "Unload Project"
    • Right click on the project node in Solution Explorer
    • Select "Edit MyApp.csproj"

    Move the <OutputType ../> property group Xml element from the <PropertyGroup .../> Xml element without Condition to the property group with conditions corresponding to build configuration / platform.

    Before:

    <Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        ...
        <OutputType>Exe</OutputType>
        ...
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        ...
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        ...
      </PropertyGroup>
    

    After:

    <Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        ...
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        ...
        <OutputType>Exe</OutputType>
        ...
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        ...
        <OutputType>WinExe</OutputType>
        ...
      </PropertyGroup>
    

    And finish:

    • Right click on the project node in Solution Explorer
    • Select "Reload Project"

    Here is a proof example:

    class Program
    {
        public static void Main(string[] args)
        {
    #if DEBUG
            Console.WriteLine("test");
    #else
            Application.Run(new Form1());
    #endif
        }
    }
    

    It works, but I don't think this is officially supported, so use at your own risk :-)

    0 讨论(0)
提交回复
热议问题