如何判断是否有控制台 [英] How to tell if there is a console

查看:141
本文介绍了如何判断是否有控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些库控制台和wpf应用程序使用的库代码。在库代码中,有一些 Console.Read()调用。我只想做那些输入读取,如果应用程序是一个控制台应用程序不是如果它的GUI应用程序 - 如何告诉在DLL如果应用程序有一个控制台?

Ive 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 its a GUI app - how to tell in dll if the app has a console?

推荐答案

最后我做了如下:

// 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();

遵循thekips通知我添加了一个委托成员到库类以获得用户验证,使用上面的检查控制台和如果存在的默认隐式使用它来获得用户验证或不做什么,如果没有(没有用户验证的动作)。这意味着:

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. 所有现有客户端(命令行应用程序,Windows服务(无用户交互),wpf应用程序)

  2. 任何需要输入的非控制台应用程序都可以使用其他(GUI - msg框等)验证替换默认代理。

感谢所有回复者。

这篇关于如何判断是否有控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆