
{"id":2470,"date":"2017-12-21T17:01:59","date_gmt":"2017-12-21T22:01:59","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=2470"},"modified":"2020-01-13T10:20:21","modified_gmt":"2020-01-13T15:20:21","slug":"how-to-do-console-output-in-wpf","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=2470","title":{"rendered":"How to do console output in WPF"},"content":{"rendered":"<p>If you wish to access standard input and output in your WPF app, you will have to compile it as a Console app and fire up the WPF application object manually.<\/p>\n<p>Sample application: <a href=\"https:\/\/github.com\/ikriv\/WpfStdOut\">https:\/\/github.com\/ikriv\/WpfStdOut<\/a>.<\/p>\n<p>Attaching to the parent process console, as proposed by <a href=\"http:\/\/www.csharp411.com\/console-output-from-winforms-application\/\">this CSharp411 article<\/a> (thanks to <a href=\"https:\/\/www.roelvanlisdonk.nl\/2012\/11\/16\/how-to-redirect-console-writeline-from-a-wpf-or-windows-forms-c-application-to-the-launching-command-prompt-console\/\">Roel van Lisdonk<\/a> for the pointer) insists on writing to the actual console and does not work well with redirected output.<\/p>\n<p>Steps to apply to your application:<\/p>\n<ol>\n<li>Create WPF application as you would normally.<\/li>\n<li>You can use <code>Console.Out.WriteLine()<\/code>, but it would not go anywhere.<\/li>\n<li>Add <code>Program.cs<\/code> file with the following boilerplate code:\n<pre style=\"font-family: Consolas; font-size: 13; color: black; background: white; lineheight: 13;\">[STAThread]\r\n<span style=\"color: blue;\">public<\/span>\u00a0<span style=\"color: blue;\">static<\/span>\u00a0<span style=\"color: blue;\">void<\/span>\u00a0Main(<span style=\"color: blue;\">string<\/span>[]\u00a0args)\r\n{\r\n\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue;\">var<\/span>\u00a0app\u00a0=\u00a0<span style=\"color: blue;\">new<\/span>\u00a0<span style=\"color: #2b91af;\">App<\/span>();\r\n\u00a0\u00a0\u00a0\u00a0app.InitializeComponent();\r\n\u00a0\u00a0\u00a0\u00a0app.Run();\r\n}<\/pre>\n<\/li>\n<li>Right click on the project, and choose Properties\u2192Application.<\/li>\n<li>Change &#8220;Startup object&#8221; to <code><i>YourNamespace<\/i>.Program<\/code>.<\/li>\n<li>Change &#8220;Output type&#8221; to &#8220;Console Application&#8221;&gt;.<\/li>\n<\/ol>\n<p>The downside is that when you run the WPF app from a shortcut, it will show a black console window in the background. This window can be hidden as described <a href=\"https:\/\/stackoverflow.com\/questions\/3571627\/show-hide-the-console-window-of-a-c-sharp-console-application\">in this StackOverflow article<\/a>.<\/p>\n<p>This is not a major problem though, since the purpose of the console output is to either show it in the command line window, or to redirect it to a file or some stream. Push come to shove, one can create a start that launches the &#8220;console&#8221; application with a detached console (<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.diagnostics.processstartinfo.createnowindow(v=vs.110).aspx\">ProcessStartInfo.CreateNoWindow<\/a>).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you wish to access standard input and output in your WPF app, you will have to compile it as a Console app and fire up the WPF application object <a href=\"https:\/\/ikriv.com\/blog\/?p=2470\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[3],"tags":[],"class_list":["entry","author-ikriv","post-2470","post","type-post","status-publish","format-standard","category-dotnet"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2470"}],"version-history":[{"count":6,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2470\/revisions"}],"predecessor-version":[{"id":4641,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2470\/revisions\/4641"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}