在项目属性的应用程序选项卡(红色)的顶部有2个灰色的下拉框(绿色),我想要使用它们。 我希望根据当前的构建配置更改输出类型。当我进行“调试构建”时,我希望该项目成为“控制台应用程序”,以便使用控制台进行调试输出。 当我进行“发布构建”时,我希望该项目成为“Windows应用程序”。 那么如何启用这些灰色的框? 注意:这是一个C#应用程序。
您无法强制Visual Studio启用这些选项,但是您可以通过手动编辑csproj文件来实现该效果。MSBuild非常强大,Visual Studio往往会隐藏其功能以简化基本用例。要编辑csproj文件,请右键单击项目并选择卸载项目,然后再次右键单击并选择编辑。控制台应用程序的标准csproj文件开头可能看起来像这样:
<?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>
注意OutputType元素。这是我们想要更改的元素。值“Exe”对应于控制台应用程序,而“Winexe”对应于Visual Studio UI中的Windows应用程序。有几种方法可以做到这一点,但最简单的可能是使用Choose元素。将此部分编辑为以下内容:
<?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>
您不能根据配置更改项目的类型。因此,控制台应用程序必须是控制台应用程序,Windows 应用程序必须是 Windows 应用程序。
您可能想要做的是创建这样一个解决方案:
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)
你可以选择同时为debug和release构建所有三个应用程序,或者使用解决方案配置管理器仅为debug构建控制台应用程序,为release构建Windows应用程序。
好的,既然Aaron告诉我这是不可能的,那么在运行时隐藏控制台窗口,当DEBUG变量未设置时,我解决了它。
我使用了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
[...]
}