Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

c# - Set application output type programmatically

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. I would like to hide the console when compiling the application for release. Not showing the console can easily be done by going into the project properties, the application tab and change the output type to windows application. When doing that only the MOGRE window will be shown.

While I believe it would be cleaner to create a windows application and attach a console to it when one wants that behavior I am still curious weather it is possible to do this programmatically.

That is, is there a way to programmatically determine that when compiling in debug mode the application compiles as command line application and when in release mode it compiles as windows application? And if so, how could it be done?

Edit: I am not asking how to attach a console to a windows forms application. I put the important part in italics out of hope that will make clearer what I want.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 :-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...