Ricibob
Ricibob

Reputation: 7705

How to tell if there is a console

I've some library code that is used by both console and WPF apps. In the library code, there are some Console.Read() calls. I only want to do those input reads if the app is a console app not if it's a GUI app - how to tell in the dll if the app has a console?

Upvotes: 29

Views: 28292

Answers (11)

Wizou
Wizou

Reputation: 1907

There are already a lot of (unsatisfying) answers... Here is a very simple and elegant one:

if (Console.LargestWindowWidth != 0) { /* we have a console */ }

LargestWindowWidth doesn't throw and returns 0 when there is no Console window.

(otherwise, I think it's safe to assume it will never return 0)

Upvotes: 6

Sergio Cabral
Sergio Cabral

Reputation: 6966

I rewrote @Ricibob's answer

public bool console_present {
    get {
        try { return Console.WindowHeight > 0; }
        catch { return false; }
    }
}

//Usage
if (console_present) { Console.Read(); }

It is simpler, but I prefer this native implementation:

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

//Usage
if (GetConsoleWindow() != IntPtr.Zero) { Console.Read(); }

Upvotes: 4

Steve B
Steve B

Reputation: 37660

If you want a good design, abstract the GUI dependences using an interface.

Implement a concrete class for the console version, another for the WPF version, and inject the correct version using any way (dependency injection, inversion of control, etc).

Upvotes: 0

thekip
thekip

Reputation: 3768

You should fix this in your design. This is a nice example of a place in which inversion of control would be very handy. As the calling code is aware of which UI is available, this code should specify an instance of an IInputReader interface, for example. This way, you can use the same code for multiple scenarios for getting input from the user.

Upvotes: 4

matrix
matrix

Reputation: 3080

This SO question may provide you a solution.

Another solution is:

Console.Read() returns -1 in windows forms applications without opening up a console window. In a console app, it returns the actual value. So you can write something like:

int j = Console.Read();
if (j == -1)
    MessageBox.Show("It's not a console app");
else
    Console.WriteLine("It's a console app");

I tested this code on console and winforms apps. In a console app, if the user inputs '-1', the value of j is 45. So it will work.

Upvotes: 2

Vilhelm H.
Vilhelm H.

Reputation: 1335

This is a modern (2018) answer to an old question.

var isReallyAConsoleWindow = Environment.UserInteractive && Console.Title.Length > 0;

The combination of Environment.UserInteractive and Console.Title.Length should give a proper answer to the question of whether there is a console window. It is a simple and straightforward solution.

Upvotes: 1

Sergio Cabral
Sergio Cabral

Reputation: 6966

This works for me (using native method).

First, declare:

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

After that, check with elegance... hahaha...:

if (GetConsoleWindow() != IntPtr.Zero)
{
    Console.Write("has console");
}

Upvotes: 25

Pedro
Pedro

Reputation: 3781

if (Environment.UserInteractive)
{
    // A console is opened
}

See: http://msdn.microsoft.com/en-us/library/system.environment.userinteractive(v=vs.110).aspx

Gets a value indicating whether the current process is running in user interactive mode.

Upvotes: 24

Ricibob
Ricibob

Reputation: 7705

In the end I did as follows:

// Property:
private bool? _console_present;
public bool console_present {
    get {
        if (_console_present == null) {
            _console_present = true;
            try { int window_height = Console.WindowHeight; }
            catch { _console_present = false; }
        }
        return _console_present.Value;
    }
}

//Usage
if (console_present)
    Console.Read();

Following thekips advice I added a delegate member to library class to get user validation - and set this to a default implimentation that uses above to check if theres a console and if present uses that to get user validation or does nothing if not (action goes ahead without user validation). This means:

  1. All existing clients (command line apps, windows services (no user interaction), wpf apps) all work with out change.
  2. Any non console app that needs input can just replace the default delegate with someother (GUI - msg box etc) validation.

Thanks to all who replied.

Upvotes: 20

Shadow Wizard
Shadow Wizard

Reputation: 66389

You can use this code:

public static bool HasMainWindow()
{
    return (Process.GetCurrentProcess().MainWindowHandle != IntPtr.Zero);
}

Worked fine with quick test on Console vs. WinForms application.

Upvotes: 12

Vano Maisuradze
Vano Maisuradze

Reputation: 5899

You can pass argument on initialize.

for example:

In your library class, add constructor with 'IsConsole' parameter.

public YourLibrary(bool IsConsole)
{
  if (IsConsole)
  {
     // Do console work
  }
  else 
  {
     // Do wpf work
  }
}

And from Console you can use:

YourLibrary lib = new YourLibrary(true);

Form wpf:

YourLibrary lib = new YourLibrary(false);

Upvotes: 3

Related Questions