0

I have a gui application.

I start it from a console (windows cmd).

Is there way to write something to console from the application?

I tried Console.WriteLine() and Debug.WriteLine(), but it doesn't work: I see that messages in Output window in VisualStudio, but not in cmd / PowerShell.

fat
  • 6,435
  • 5
  • 44
  • 70
  • 1
    So you have two applications, one forms and one console? – TaW Oct 13 '18 at 08:27
  • @TaW no, I have only one gui application. But I start it from cmd window. – fat Oct 13 '18 at 08:29
  • You would have to find the/a cmd process window handle and inject text there. - Maybe [this](https://stackoverflow.com/questions/25772622/how-do-i-echo-into-an-existing-cmd-window) is of interest. – TaW Oct 13 '18 at 08:33
  • @John: There is no console application. Only gui one. But I start it not from Visual Studio or Windows explorer but from console (window cmd or powershell) – fat Oct 13 '18 at 08:33
  • 2
    You could change your projects outputtype (target) to [exe](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/target-exe-compiler-option), that gives you a console that you can write to with Console.WriteLine() – rene Oct 13 '18 at 08:36
  • @rene: Exactly! Please, post an answer, I'll respectfully accept it. – fat Oct 13 '18 at 09:32

2 Answers2

3

The best solution would be the simplest one. Create a Windows Form project and then change the output type to console application:

Project Properties -> Application -> Output Type -> Console Application

then you will have the Console.WriteLine to work.

roozbeh S
  • 1,084
  • 1
  • 9
  • 16
1

As stated already, you could change the project to be a Console application by right clicking on the Project and clicking on the Project Properties > Application > Output Type. Then you can set this to Console application and have the ability to use Console.WriteLine.

However, if this is plainly for debugging, consider using Debug.WriteLine (which will write to the debut console inside of Visual Studio).

Finally, if you just want a convenient and easy way to display messages like you can in the console, look into MessageBox.Show

dhilln
  • 56
  • 3