如何打印到WPF应用程序的启动控制台?

时间:2019-05-06 10:34:11

标签: c# wpf console

我有一个WPF窗口应用程序。 有一个新请求要求启用应用程序的命令行参数。

  1. 从命令行启动应用程序(调用app.exe)时不带参数,应该启动GUI(与以前一样)

  2. 使用GUI开关(例如app.exe -gui)从命令行启动应用程序应该会像以前一样启动GUI

  3. 从命令行启动应用程序并进行其他切换(例如app.exe -f a -b e)应在控制台模式(无窗口)下运行该应用程序

我浏览了多个帖子,例如Start WPF Application in Console Application(以及类似的文章),No output to console from a WPF application,并回顾了命令行解析器实用程序,例如Commandline Parser

实现上述所有功能(请参见下面的代码)时,在选项3中,应用程序正在新的控制台进程(新的cmd窗口)中运行,因此错误的参数或--help开关会立即打开新窗口它正在关闭。

如何使用从中调用应用程序的控制台?因此,当用户打开新的cmd窗口并键入app.exe --help时,将不会打开新的控制台进程/窗口。

[SuppressUnmanagedCodeSecurity]
public static class ConsoleManager
{
    private const string Kernel32_DllName = "kernel32.dll";

    [DllImport(Kernel32_DllName)]
    private static extern bool AllocConsole();

    [DllImport(Kernel32_DllName)]
    private static extern bool FreeConsole();

    [DllImport(Kernel32_DllName)]
    private static extern IntPtr GetConsoleWindow();

    [DllImport(Kernel32_DllName)]
    private static extern int GetConsoleOutputCP();

    public static bool HasConsole
    {
        get { return GetConsoleWindow() != IntPtr.Zero; }
    }

    /// <summary>
    /// Creates a new console instance if the process is not attached to a console already.
    /// </summary>
    public static void Show()
    {
        //#if DEBUG
        if (!HasConsole)
        {
            AllocConsole();
            InvalidateOutAndError();
        }
        //#endif
    }

    /// <summary>
    /// If the process has a console attached to it, it will be detached and no longer visible. Writing to the System.Console is still possible, but no output will be shown.
    /// </summary>
    public static void Hide()
    {
        if (HasConsole)
        {
            SetOutAndErrorNull();
            FreeConsole();
        }
    }

    static void InvalidateOutAndError()
    {
        Type type = typeof(System.Console);

        System.Reflection.FieldInfo _out = type.GetField("_out",
            System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);

        System.Reflection.FieldInfo _error = type.GetField("_error",
            System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);

        System.Reflection.MethodInfo _InitializeStdOutError = type.GetMethod("InitializeStdOutError",
            System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);

        Debug.Assert(_out != null);
        Debug.Assert(_error != null);

        Debug.Assert(_InitializeStdOutError != null);

        _out.SetValue(null, null);
        _error.SetValue(null, null);

        _InitializeStdOutError.Invoke(null, new object[] { true });
    }

    static void SetOutAndErrorNull()
    {
        Console.SetOut(TextWriter.Null);
        Console.SetError(TextWriter.Null);
    }
}

我的Starter类中的代码

 [STAThread]
 public static void Main(string[] args)
 {
        // enable the console
        ConsoleManager.Show();

        parser.ExtractArgumentAttributes(CommandLineArguments);

        try
        {
            parser.ParseCommandLine(args);                

            if (parser.ParsingSucceeded)
            {
                CommandLineArguments.AnalyzeOptions();
                if (CommandLineArguments.OpenMode == CmdLineArguments.UI_TYPE.GUI)
                {
                    // GUI usage model                        
                    ConsoleManager.Hide();  // shutdown the console                        
                    App.Main();             // start up the UI
                }
                else // command line usage
                {
                    // do stuff without a GUI
                }
            }                
        }
        catch (CommandLineException parser_exception)
        {
            Console.WriteLine(parser_exception.Message);
            parser.ShowUsage();
        }
  }

0 个答案:

没有答案