Visual Studio / WPF > WindowsアプリケーションにてSystem.Console.WriteLine()をコンソールに出力する


動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2015 Community

コンソールアプリケーションでないプロジェクトにおいては、System.Console.WriteLine()はコマンドプロンプトに出力されない。
Windowsアプリケーションで後からコマンドプロンプト出力するには。

対象コード

MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace _170618_t0900_consoleOutput
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            System.Console.WriteLine("hello");
        }
    }
}
MainWindow.xaml
<Window x:Class="_170618_t0900_consoleOutput.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_170618_t0900_consoleOutput"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="button1" Click="button1_Click" 
                Height="28" Width="100"
                Content="button"/>
    </Grid>
</Window>

手順

参考: https://stackoverflow.com/questions/4480930/why-doesnt-console-writeline-console-write-work-in-visual-studio-express

answered Nov 30 '12 at 20:58
ibrahim

  1. ソリューションエクスプローラーからプロパティを開く
  2. アプリケーションタブの「出力の種類」を「コンソールアプリケーション」に変更する

実行例

アプリケーション起動時にコマンドプロンプトとアプリケーション(例:MainWindow)が開く。

MainWindowのボタン押下ごとにコマンドプロンプトに出力される。