問題描述
WPF 應用程序沒有輸出到控制台? (No output to console from a WPF application?)
我在一個非常簡單的 WPF 測試應用程序中使用 Console.WriteLine()
,但是當我從命令行執行該應用程序時,我沒有看到任何內容被寫入控制台。有誰知道這裡會發生什麼?
我可以通過在 VS 2008 中創建一個 WPF 應用程序來重現它,並在任何地方簡單地添加 Console.WriteLine("text")
它被執行的地方。有什麼想法嗎?
我現在只需要像 Console.WriteLine()
這樣簡單的東西。我意識到我可以使用 log4net 或其他一些日誌記錄解決方案,但我真的不需要這個應用程序的那麼多功能。
編輯: 我應該記得 Console.WriteLine()
用於控制台應用程序。哦,好吧,沒有愚蠢的問題,對吧?:‑) 我現在只使用 System.Diagnostics.Trace.WriteLine()
和 DebugView。
參考解法
方法 1:
You can use
Trace.WriteLine("text");
This will output to the "Output" window in Visual Studio (when debugging).
make sure to have the Diagnostics assembly included:
using System.Diagnostics;
方法 2:
Right click on the project, "Properties", "Application" tab, change "Output Type" to "Console Application", and then it will also have a console.
方法 3:
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).
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.
With the following class, you just need to write ConsoleManager.Show()
somewhere before any call to Console.Write
...
[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);
}
}
方法 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:
Include this:
using System.Diagnostics;
And then:
Debug.WriteLine("something");
方法 5:
Although John Leidegren keeps shooting down the idea, Brian is correct. I've just got it working in Visual Studio.
To be clear a WPF application does not create a Console window by default.
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.
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.
(by Rob、Phobis、Brian、John Leidegren、Smitty、antidemon)