没有输出从一个WPF应用程序控制台?
我使用Console.WriteLine()从非常简单的WPF测试应用程序,但当我从命令行执行应用程序,我看到什么被写到控制台。有谁知道什么可能会在这里吗?
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?
我可以通过在VS 2008 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?
我所需要的,现在是一些简单的Console.WriteLine()。我知道我可以用log4net的或舒美特其他记录解决方案,但我真的不需要那么多的功能,这个应用程序。
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()用于控制台应用程序。哦,没有愚蠢的问题,对不对? :-) 我就用System.Diagnostics.Trace.WriteLine()和DebugView中现在。
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.Write方法。这将初始化控制台来正常工作而不改变项目类型(这对WPF应用程序将无法正常工作)。
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).
下面是如何ConsoleManager类可能看起来像一个完整的源$ C $ C例如,以及如何可以用来启用/禁用控制*立于项目类型的。
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.
通过下面的类,你只需要编写 ConsoleManager.Show()
任何调用之前某处 Console.Write
...
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);
}
}