winform
あるアプリケーションのソース コードを見たことがありますが、そのコードにはConsole.WriteLine();
. その理由を尋ねたところ、デバッグ目的であると言われました。
Plsの本質とは何ですかConsole.WriteLine();
?winform
winform
あるアプリケーションのソース コードを見たことがありますが、そのコードにはConsole.WriteLine();
. その理由を尋ねたところ、デバッグ目的であると言われました。
Plsの本質とは何ですかConsole.WriteLine();
?winform
コンソールに書き込みます。
エンド ユーザーには表示されません。正直なところ、ログを適切なログに記録する方がはるかにクリーンですが、VS を介して実行すると、コンソール ウィンドウにデータが表示されます。
Winforms は、ウィンドウを表示する単なるコンソール アプリです。デバッグ情報をコンソール アプリに送信できます。
以下の例でわかるように、親ウィンドウをアタッチし、そこに情報を送り込むコマンドがあります。
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MyWinFormsApp
{
static class Program
{
[DllImport( "kernel32.dll" )]
static extern bool AttachConsole( int dwProcessId );
private const int ATTACH_PARENT_PROCESS = -1;
[STAThread]
static void Main( string[] args )
{
// redirect console output to parent process;
// must be before any calls to Console.WriteLine()
AttachConsole( ATTACH_PARENT_PROCESS );
// to demonstrate where the console output is going
int argCount = args == null ? 0 : args.Length;
Console.WriteLine( "nYou specified {0} arguments:", argCount );
for (int i = 0; i < argCount; i++)
{
Console.WriteLine( " {0}", args[i] );
}
// launch the WinForms application like normal
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Application.Run( new Form1() );
}
}
}
この例のリソースは次のとおりです: http://www.csharp411.com/console-output-from-winforms-application/
通常は実際には使用しませんが、コンソールを接続したり、AllocConsoleを使用したりすると、他のコンソール アプリケーションと同じように機能し、そこに出力が表示されます。
迅速なデバッグには好みDebug.WriteLine
ますが、より堅牢なソリューションを得るには、Traceクラスが望ましい場合があります。
ウィンドウ本当に、彼らはConsole
にリダイレクトされない限り、何も実行されません。Output
Debug.WriteLine
代わりに活用する必要があります。
の利点は、モードDebug.WriteLine
でビルドするときに最適化されることです。Release
注: Brad Christie と Haedrian が指摘したようにConsole
、Windows フォーム アプリケーションを実行すると、実際には Visual Studio のウィンドウに書き込みます。あなたは毎日何か新しいことを学びます!