我在一个非常简单的 WPF 测试应用程序中使用
Console.WriteLine()
,但是当我从命令行执行该应用程序时,我没有看到任何内容写入控制台。有谁知道这里会发生什么?
我可以通过在 VS 2008 中创建一个 WPF 应用程序来重现它,然后简单地在执行它的任何地方添加
Console.WriteLine("text")
。有什么想法吗?
我现在所需要的只是像
Console.WriteLine()
一样简单。我意识到我可以使用 log4net 或其他日志记录解决方案,但我真的不需要这个应用程序那么多的功能。
编辑:我应该记住
Console.WriteLine()
适用于控制台应用程序。哦,好吧,没有愚蠢的问题,对吧? :-)
我现在只使用 System.Diagnostics.Trace.WriteLine()
和 DebugView。
你可以使用
Trace.WriteLine("text");
这将输出到 Visual Studio 中的“输出”窗口(调试时)。
确保包含诊断组件:
using System.Diagnostics;
右键单击项目,“属性”,“应用程序”选项卡,将“输出类型”更改为“控制台应用程序”,然后它也有一个控制台,WPF应用程序仍然按预期运行(即使应用程序输出类型切换为“控制台应用程序”)。
在实际调用任何 Console.Write 方法之前,您必须手动创建控制台窗口。这将使控制台正常工作,而无需更改项目类型(这对于 WPF 应用程序不起作用)。
这是一个完整的源代码示例,展示了 ConsoleManager 类的外观,以及如何使用它来启用/禁用控制台,而与项目类型无关。
在下面的课程中,您只需在调用
ConsoleManager.Show()
...之前在某处编写
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);
}
}
旧帖子,但我遇到了这个问题,所以如果您尝试在 Visual Studio 中的 WPF 项目中输出某些内容,现代方法是:
包括这个:
using System.Diagnostics;
然后:
Debug.WriteLine("something");
尽管约翰·莱德格伦 (John Leidegren) 不断否定这个想法,但布莱恩 (Brian) 是正确的。我刚刚在 Visual Studio 中运行它。
需要明确的是,WPF 应用程序默认情况下不会创建控制台窗口。
您必须创建一个 WPF 应用程序,然后将 OutputType 更改为“控制台应用程序”。当您运行该项目时,您将看到一个控制台窗口,前面有您的 WPF 窗口。
它看起来不太漂亮,但我发现它很有帮助,因为我希望我的应用程序从命令行运行并带有反馈,然后对于某些命令选项,我将显示 WPF 窗口。
我在输出窗口中使用 Console.WriteLine()...
Brian 的解决方案 是在 WPF 应用程序启动时“始终”打开控制台。如果您想动态地启用控制台输出(例如,仅当使用某些命令行参数启动时),请调用AttachConsole
:
[DllImport("kernel32.dll")]
static extern bool AttachConsole(uint dwProcessId);
const uint ATTACH_PARENT_PROCESS = 0x0ffffffff;
然后,当您想开始写入控制台时:
AttachConsole(ATTACH_PARENT_PROCESS);
Console.WriteLine("Hello world!");
Console.WriteLine("Writing to the hosting console!");
它是一种表单,包含一个标签和一个文本框。控制台输出被重定向到文本框。
还有一个名为 ConsoleView 的类,它实现了三个公共方法:Show()、Close() 和 Release()。最后一个是打开控制台并激活“关闭”按钮以查看结果。
该表单称为 FrmConsole。这是 XAML 和 C# 代码。
使用非常简单:
ConsoleView.Show("Title of the Console");
用于打开控制台。用途:
System.Console.WriteLine("The debug message");
用于将文本输出到控制台。
用途:
ConsoleView.Close();
用于关闭控制台。
ConsoleView.Release();
打开控制台并启用关闭按钮
XAML
<Window x:Class="CustomControls.FrmConsole"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CustomControls"
mc:Ignorable="d"
Height="500" Width="600" WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Topmost="True" Icon="Images/icoConsole.png">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="*"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Name="lblTitulo" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="Titulo"/>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="1" Name="txtInner" FontFamily="Arial" FontSize="10" ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" TextWrapping="Wrap"/>
</Grid>
<Button Name="btnCerrar" Grid.Row="2" Content="Cerrar" Width="100" Height="30" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center"/>
</Grid>
窗口代码:
partial class FrmConsole : Window
{
private class ControlWriter : TextWriter
{
private TextBox textbox;
public ControlWriter(TextBox textbox)
{
this.textbox = textbox;
}
public override void WriteLine(char value)
{
textbox.Dispatcher.Invoke(new Action(() =>
{
textbox.AppendText(value.ToString());
textbox.AppendText(Environment.NewLine);
textbox.ScrollToEnd();
}));
}
public override void WriteLine(string value)
{
textbox.Dispatcher.Invoke(new Action(() =>
{
textbox.AppendText(value);
textbox.AppendText(Environment.NewLine);
textbox.ScrollToEnd();
}));
}
public override void Write(char value)
{
textbox.Dispatcher.Invoke(new Action(() =>
{
textbox.AppendText(value.ToString());
textbox.ScrollToEnd();
}));
}
public override void Write(string value)
{
textbox.Dispatcher.Invoke(new Action(() =>
{
textbox.AppendText(value);
textbox.ScrollToEnd();
}));
}
public override Encoding Encoding
{
get { return Encoding.UTF8; }
}
}
//DEFINICIONES DE LA CLASE
#region DEFINICIONES DE LA CLASE
#endregion
//CONSTRUCTORES DE LA CLASE
#region CONSTRUCTORES DE LA CLASE
public FrmConsole(string titulo)
{
InitializeComponent();
lblTitulo.Content = titulo;
Clear();
btnCerrar.Click += new RoutedEventHandler(BtnCerrar_Click);
Console.SetOut(new ControlWriter(txtInner));
DesactivarCerrar();
}
#endregion
//PROPIEDADES
#region PROPIEDADES
#endregion
//DELEGADOS
#region DELEGADOS
private void BtnCerrar_Click(object sender, RoutedEventArgs e)
{
Close();
}
#endregion
//METODOS Y FUNCIONES
#region METODOS Y FUNCIONES
public void ActivarCerrar()
{
btnCerrar.IsEnabled = true;
}
public void Clear()
{
txtInner.Clear();
}
public void DesactivarCerrar()
{
btnCerrar.IsEnabled = false;
}
#endregion
}
ConsoleView类的代码
static public class ConsoleView
{
//DEFINICIONES DE LA CLASE
#region DEFINICIONES DE LA CLASE
static FrmConsole console;
static Thread StatusThread;
static bool isActive = false;
#endregion
//CONSTRUCTORES DE LA CLASE
#region CONSTRUCTORES DE LA CLASE
#endregion
//PROPIEDADES
#region PROPIEDADES
#endregion
//DELEGADOS
#region DELEGADOS
#endregion
//METODOS Y FUNCIONES
#region METODOS Y FUNCIONES
public static void Show(string label)
{
if (isActive)
{
return;
}
isActive = true;
//create the thread with its ThreadStart method
StatusThread = new Thread(() =>
{
try
{
console = new FrmConsole(label);
console.ShowDialog();
//this call is needed so the thread remains open until the dispatcher is closed
Dispatcher.Run();
}
catch (Exception)
{
}
});
//run the thread in STA mode to make it work correctly
StatusThread.SetApartmentState(ApartmentState.STA);
StatusThread.Priority = ThreadPriority.Normal;
StatusThread.Start();
}
public static void Close()
{
isActive = false;
if (console != null)
{
//need to use the dispatcher to call the Close method, because the window is created in another thread, and this method is called by the main thread
console.Dispatcher.InvokeShutdown();
console = null;
StatusThread = null;
}
console = null;
}
public static void Release()
{
isActive = false;
if (console != null)
{
console.Dispatcher.Invoke(console.ActivarCerrar);
}
}
#endregion
}
我希望这个结果有用。
解决方案,有时有效,有时无效,但来到了在所有情况下都有效的代码:
// writing to parent console https://stackoverflow.com/a/73971822
public class WriteToConsoleHelper
{
const uint parentProcessId = 0x0ffffffff;
[DllImport("kernel32.dll")]
static extern bool AttachConsole(uint dwProcessId);
public static void WriteLineToConsole(string message)
{
var currentProcessId = (uint)Environment.ProcessId;
AttachConsole(parentProcessId);
var originalConsoleOut = Console.Out;
var writer = new StreamWriter(Console.OpenStandardOutput())
{
AutoFlush = true
};
Console.SetOut(writer);
Console.WriteLine(message);
Console.SetOut(originalConsoleOut);
writer.Dispose();
AttachConsole(currentProcessId);
}
}