WPF应用程序没有输出到控制台?

时间:2021-09-12 18:09:18

I'm using Console.WriteLine() from a very simple WPF test application, but when I execute the application from the command line, I'm seeing nothing being written to the console. Does anyone know what might be going on here?

我使用的是来自一个非常简单的WPF测试应用程序的Console.WriteLine(),但是当我从命令行执行应用程序时,我看到没有任何东西被写入控制台。有人知道这是怎么回事吗?

I can reproduce it by creating a WPF application in VS 2008, and simply adding Console.WriteLine("text") anywhere that get executed. Any ideas?

我可以通过在VS 2008中创建一个WPF应用程序来复制它,并简单地在任何被执行的地方添加Console.WriteLine(“文本”)。什么好主意吗?

All I need for right now is something as simple as Console.WriteLine(). I realize I could use log4net or somet other logging solution, but I really don't need that much functionality for this application.

现在我所需要的就是简单的Console.WriteLine()。我意识到我可以使用log4net或somet其他日志解决方案,但我真的不需要这个应用程序的很多功能。

Edit: I should have remembered that Console.WriteLine() is for console applications. Oh well, no stupid questions, right? :-) I'll just use System.Diagnostics.Trace.WriteLine() and DebugView for now.

编辑:我应该记住Console.WriteLine()是用于控制台应用程序的。哦,没有愚蠢的问题,对吧?我将只使用system .诊断。trace . writeline()和DebugView。

9 个解决方案

#1


78  

You'll have to create a Console window manually before you actually call any Console.Write methods. That will init the Console to work properly without changing the project type (which for WPF application won't work).

在实际调用任何控制台之前,您必须手动创建一个控制台窗口。写方法。这将使控制台在不改变项目类型的情况下正常工作(对于WPF应用程序是无效的)。

Here's a complete source code example, of how a ConsoleManager class might look like, and how it can be used to enable/disable the Console, independently of the project type.

这里有一个完整的源代码示例,说明了一个conanager类的外观,以及它如何能够独立于项目类型启用/禁用控制台。

With the following class, you just need to write ConsoleManager.Show() somewhere before any call to Console.Write...

有了下面的课,你只需要写一个conanager . show()就可以了。

[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 DEBUG
        if (HasConsole)
        {
            SetOutAndErrorNull();
            FreeConsole();
        }
        //#endif
    }

    public static void Toggle()
    {
        if (HasConsole)
        {
            Hide();
        }
        else
        {
            Show();
        }
    }

    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);
    }
} 

#2


102  

Right click on the project, "Properties", "Application" tab, change "Output Type" to "Console Application", and then it will also have a console.

右键点击项目,“属性”,“应用”选项卡,将“输出类型”更改为“控制台应用程序”,然后它也会有一个控制台。

#3


93  

You can use

您可以使用

Trace.WriteLine("text");

This will output to the "Output" window in Visual Studio (when debugging).

这将输出到Visual Studio中的“输出”窗口(调试时)。

make sure to have the Diagnostics assembly included:

确保诊断程序集包括:

using System.Diagnostics;

#4


9  

Although John Leidegren keeps shooting down the idea, Brian is correct. I've just got it working in Visual Studio.

虽然约翰·勒德格伦一直在坚持否定这个想法,但布莱恩是正确的。我刚刚在Visual Studio中工作。

To be clear a WPF application does not create a Console window by default.

显然,WPF应用程序在默认情况下不会创建控制台窗口。

You have to create a WPF Application and then change the OutputType to "Console Application". When you run the project you will see a console window with your WPF window in front of it.

您必须创建一个WPF应用程序,然后将OutputType更改为“控制台应用程序”。当您运行这个项目时,您将看到一个控制台窗口,前面有WPF窗口。

It doesn't look very pretty, but I found it helpful as I wanted my app to be run from the command line with feedback in there, and then for certain command options I would display the WPF window.

它看起来不是很漂亮,但我发现它很有用,因为我想让我的应用程序从命令行运行,并在那里得到反馈,然后对于某些命令选项,我将显示WPF窗口。

#5


8  

It's possible to see output intended for console by using command line redirection.

可以使用命令行重定向来查看控制台的输出。

For example:

例如:

C:\src\bin\Debug\Example.exe > output.txt

will write all the content to output.txt file.

将所有内容写入输出。txt文件。

#6


4  

Old post, but I ran into this so if you're trying to output something to Output in a WPF project in Visual Studio, the contemporary method is:

旧的post,但是我遇到了这个所以如果你想要输出一些东西到WPF项目在Visual Studio中,当代的方法是:

Include this:

包括:

using System.Diagnostics;

And then:

然后:

Debug.WriteLine("something");

#7


3  

I use Console.WriteLine() for use in the Output window...

我使用Console.WriteLine()在输出窗口中使用…

#8


0  

Check out this post, was very helpful for myself. Download the code sample:

看看这篇文章,对我自己很有帮助。下载的代码示例:

http://www.codeproject.com/Articles/335909/Embedding-a-Console-in-a-C-Application

http://www.codeproject.com/Articles/335909/Embedding-a-Console-in-a-C-Application

#9


-14  

As far as I know, Console.WriteLine() is only for console applications. I think this is your problem.

据我所知,Console.WriteLine()仅用于控制台应用程序。我想这是你的问题。

#1


78  

You'll have to create a Console window manually before you actually call any Console.Write methods. That will init the Console to work properly without changing the project type (which for WPF application won't work).

在实际调用任何控制台之前,您必须手动创建一个控制台窗口。写方法。这将使控制台在不改变项目类型的情况下正常工作(对于WPF应用程序是无效的)。

Here's a complete source code example, of how a ConsoleManager class might look like, and how it can be used to enable/disable the Console, independently of the project type.

这里有一个完整的源代码示例,说明了一个conanager类的外观,以及它如何能够独立于项目类型启用/禁用控制台。

With the following class, you just need to write ConsoleManager.Show() somewhere before any call to Console.Write...

有了下面的课,你只需要写一个conanager . show()就可以了。

[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 DEBUG
        if (HasConsole)
        {
            SetOutAndErrorNull();
            FreeConsole();
        }
        //#endif
    }

    public static void Toggle()
    {
        if (HasConsole)
        {
            Hide();
        }
        else
        {
            Show();
        }
    }

    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);
    }
} 

#2


102  

Right click on the project, "Properties", "Application" tab, change "Output Type" to "Console Application", and then it will also have a console.

右键点击项目,“属性”,“应用”选项卡,将“输出类型”更改为“控制台应用程序”,然后它也会有一个控制台。

#3


93  

You can use

您可以使用

Trace.WriteLine("text");

This will output to the "Output" window in Visual Studio (when debugging).

这将输出到Visual Studio中的“输出”窗口(调试时)。

make sure to have the Diagnostics assembly included:

确保诊断程序集包括:

using System.Diagnostics;

#4


9  

Although John Leidegren keeps shooting down the idea, Brian is correct. I've just got it working in Visual Studio.

虽然约翰·勒德格伦一直在坚持否定这个想法,但布莱恩是正确的。我刚刚在Visual Studio中工作。

To be clear a WPF application does not create a Console window by default.

显然,WPF应用程序在默认情况下不会创建控制台窗口。

You have to create a WPF Application and then change the OutputType to "Console Application". When you run the project you will see a console window with your WPF window in front of it.

您必须创建一个WPF应用程序,然后将OutputType更改为“控制台应用程序”。当您运行这个项目时,您将看到一个控制台窗口,前面有WPF窗口。

It doesn't look very pretty, but I found it helpful as I wanted my app to be run from the command line with feedback in there, and then for certain command options I would display the WPF window.

它看起来不是很漂亮,但我发现它很有用,因为我想让我的应用程序从命令行运行,并在那里得到反馈,然后对于某些命令选项,我将显示WPF窗口。

#5


8  

It's possible to see output intended for console by using command line redirection.

可以使用命令行重定向来查看控制台的输出。

For example:

例如:

C:\src\bin\Debug\Example.exe > output.txt

will write all the content to output.txt file.

将所有内容写入输出。txt文件。

#6


4  

Old post, but I ran into this so if you're trying to output something to Output in a WPF project in Visual Studio, the contemporary method is:

旧的post,但是我遇到了这个所以如果你想要输出一些东西到WPF项目在Visual Studio中,当代的方法是:

Include this:

包括:

using System.Diagnostics;

And then:

然后:

Debug.WriteLine("something");

#7


3  

I use Console.WriteLine() for use in the Output window...

我使用Console.WriteLine()在输出窗口中使用…

#8


0  

Check out this post, was very helpful for myself. Download the code sample:

看看这篇文章,对我自己很有帮助。下载的代码示例:

http://www.codeproject.com/Articles/335909/Embedding-a-Console-in-a-C-Application

http://www.codeproject.com/Articles/335909/Embedding-a-Console-in-a-C-Application

#9


-14  

As far as I know, Console.WriteLine() is only for console applications. I think this is your problem.

据我所知,Console.WriteLine()仅用于控制台应用程序。我想这是你的问题。