Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write to command line from a WPF application?

Hi I know how to write to console but if I write to console in my program and call my program from the command line it won't display anything.

How do I make it so that when I say Console.WriteLine or Console.Out.Writeline ir prints to the command prompt from which it was called and not somewhere else?

Once again I know how to do Console.WriteLine so it's not that :-p unless I'm doing it wrong.

From what I can tell it's probably something to do with Console.SetOut(TextWriter t)

this is a WPF application and I need it to post its data to the command line while still retaining the GUI at startup. I've triple checked and my code hits the print lines, I can actually see the lines being printed to the Visual Studio output window, it just won't display in the command line when I run it manually without VS.

If possible I need to conditionally have the console display. ie if run from command line (or even with command arguments), display or post to the prompt, otherwise do not.

like image 518
ben Avatar asked Jun 16 '10 23:06

ben


People also ask

What are WPF commands?

Commanding is an input mechanism in Windows Presentation Foundation (WPF) which provides input handling at a more semantic level than device input. Examples of commands are the Copy, Cut, and Paste operations found on many applications.

Can WPF be used with C++?

WPF is a . NET technology. Of course it can be used with C++, like any other part of . NET can, but it requires you to jump through some interop hoops, or possibly write it all in C++/CLI.

Where does the execution start in a WPF application?

For a WPF standalone application that is generated in Visual Studio using the New Project wizard, the entry point for the application is the Main function, defined in App. g. cs (generated code). In the default project, this is the public static void App.

How do I run a WPF file in Linux?

Option 1: . NET Core 3.0's support for WPF, a WPF application can run on Linux under Wine. Wine is a compatibility layer which allows Windows applications on Linux and other OSes, including . NET Core Windows applications.


2 Answers

This is actually trivial:

public void WriteToConsole(string message)
{
  AttachConsole(-1);
  Console.WriteLine(message);
}
[DllImport("Kernel32.dll")]
public static extern bool AttachConsole(int processId);

This method will write your message to the console if your program was started from the command line, otherwise it will do nothing.

If you want to use an alternative output mechanism when you weren't started from the command line you can do it this way:

public void WriteToConsole(string message)
{
  _connected = _connected || AttachConsole(-1);
  if(_connected)
    Console.WriteLine("Hello");
  else
    ... other way to output message ...
}
bool _connected;
[DllImport("Kernel32.dll")]
public static extern bool AttachConsole(int processId);
like image 164
Ray Burns Avatar answered Sep 18 '22 08:09

Ray Burns


The full code for this particular task is:

    public static void WriteToConsole(string message)
    {
        AttachConsole(-1);
        System.Console.WriteLine(message);
        SendKeys.SendWait("{ENTER}");
        FreeConsole();
    }

    [DllImport("Kernel32.dll")]
    private static extern bool AttachConsole(int processId);

    [DllImport("kernel32.dll")]
    private static extern bool FreeConsole();

All credits goes to Ray Burns & Scott Marlowe.

like image 31
KUTlime Avatar answered Sep 18 '22 08:09

KUTlime