3

At the top of the Apllication-Tab(red) of the project properties there are 2 grayed out dropdown boxes(green), which I would like to use.

Project properties

I want to change the output type depending on the current build configuration. When I make a Debug-build I want the project to be a Console Application to use the console for debug output. When I make a Release-build I want the project to be a Windows Application.

So how can I enable the grayed out boxes?

Note: It's a C#-Application

LostPhysx
  • 3,573
  • 8
  • 43
  • 73
  • Console applications are very different from winforms applications. It sounds like you just want to be able to output debug messages from your winforms app. You can do this by using a trace listener and putting Debug or Trace calls in your code. see http://msdn.microsoft.com/en-us/library/sk36c28t(v=vs.110).aspx – Igby Largeman Jul 25 '14 at 00:31
  • @IgbyLargeman: Could you please share an answer where you show how to do this in addition to this link? Links can expire(especially microsoft links) – LostPhysx Jul 25 '14 at 00:44

3 Answers3

5

You can't force Visual Studio to enable the options, but you can achieve the effect by manually editing the csproj file. MSBuild is really quite powerful and Visual Studio tends to hide its functionality to simplify basic use cases. To edit the csproj file, right click the project and select Unload Project, the right click again and select Edit. A standard csproj for a console app would look something like this at the top

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{DE6C0E52-0B46-42E1-BA10-83C0B7B08A7F}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>ServiceSample</RootNamespace>
    <AssemblyName>ServiceSample</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
  </PropertyGroup>

Note the element OutputType. That is the one we want to change. The value "Exe" corresponds to a console application while "Winexe" corresponds to a Windows application in the Visual Studio UI. There are a few ways to do this, but the simplest is probably with the Choose element. Edit this section to look like this:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{DE6C0E52-0B46-42E1-BA10-83C0B7B08A7F}</ProjectGuid>
    <!--<OutputType>Exe</OutputType>-->
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>ServiceSample</RootNamespace>
    <AssemblyName>ServiceSample</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
  </PropertyGroup>
  <Choose>
    <When Condition=" '$(Configuration)' == 'Debug' ">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
      </PropertyGroup>
    </When>
    <Otherwise>
      <PropertyGroup>
        <OutputType>Winexe</OutputType>
      </PropertyGroup>
    </Otherwise>
  </Choose>

Note that we commented OutputType from the main PropertyGroup and put into to sections within the Choose element, which will select the values based on the value of the Configuration property.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
2

You can't change the TYPE of project based on the configuration. So a console app has to be a console app, a windows app has to be a windows app.

What you may want to do is may a solution like this:

MySolution
    Class Library Project (.DLL that contains all the code)
    Console Application (.EXE that references the above .DLL)
    Windows Application (.EXE that references the above .DLL)

Then you can either build all three for both debug and release, or you can use the solution configuration manager to only build the console app for debug, and the windows app for release.

Aaron
  • 435
  • 3
  • 5
  • +1 for creativity, but as this should become a game which is already complex enough this would be a mess to integrate into my project. Also the Console Application would have the same Windows like the Windows application but has a Console additionally. I could try to hide the Console programmatically if the DEBUG variable is not set... – LostPhysx Jul 25 '14 at 00:22
  • @Paedow: console apps don't have windows. – Igby Largeman Jul 25 '14 at 00:26
  • If you create the Project as Windows Forms project and change the output type to console application you can use console AND winforms. You can also create the project as console application and manually add a reference to Sys.Win.Forms and create Forms – LostPhysx Jul 25 '14 at 00:31
0

Ok, since Aaron told me this is impossible I solved it through hiding the console window during runtime when the DEBUG variable is not set.

I used a piece of code from another question on Stackoverflow:

using System.Runtime.InteropServices;

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

const int SW_HIDE = 0;
const int SW_SHOW = 5;

static void Main(string[] args)
{
#if !DEBUG
    var handle = GetConsoleWindow();
    ShowWindow(handle, SW_HIDE);
#endif
[...]
}
Community
  • 1
  • 1
LostPhysx
  • 3,573
  • 8
  • 43
  • 73