How to tell if there is a console

前端 未结 10 2213
庸人自扰
庸人自扰 2020-12-14 16:34

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 rea

10条回答
  •  抹茶落季
    2020-12-14 17:14

    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.

提交回复
热议问题