8

我想添加一个额外的控制台窗口来记录我的 wpf 应用程序中的 realtine 信息。任何的想法??

巴约

答案: 项目属性中的控制台应用程序对我有用。谢谢

4

5 回答 5

5

不要这样做。

查看log4netNLog以将日志输出到文件中。通过正确配置这些框架,您可以获得更多功能(不同的日志级别、自动时间戳、每个记录行前面的自动类名)

当您使用它时,您可能还想实现自己的外观,以将使用过的日志框架隐藏在您的其余代码中。这将允许您在需要时轻松更改日志框架。


如果您想为您的程序同时拥有一个控制台和一个 GUI 窗口,您可以通过将项目编译为console application( csc /target:exe) 来实现此行为。但请注意:这肯定会导致可用性不佳,因为没有用户会期望您的应用程序同时具有控制台和 GUI 窗口。

于 2011-09-26T17:21:26.547 回答
5

您可以调用AttachConsole WIN API 函数,然后使用 PInvoke 调用此函数:

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool AttachConsole(uint dwProcessId);

const uint ATTACH_PARENT_PROCESS = 0x0ffffffff;  // default value if not specifing a process ID

// Somewhere in main method
AttachConsole(ATTACH_PARENT_PROCESS);
于 2011-09-26T18:12:06.583 回答
1

谢谢你上面的想法。以下是将控制台窗口添加到 WPF 应用程序所需的所有步骤。我们修改了 WPF 测试应用程序,以便可以在夜间测试过程中从命令行调用它。唯一的故障是当应用程序从控制台运行时,在调用 FreeConsole() 并且我们的应用程序退出后,命令提示符不会立即写入控制台窗口。FreeConsole() 函数似乎缺少对 Flush() 类函数的调用,以强制将命令提示符写入控制台窗口。我的理由是控制台窗口向上/向下箭头历史可用,并且控制台接受另一个命令,但是当下一个应用程序运行并写入控制台窗口时,缺少的命令提示符会用它写入。

  1. 在项目属性 Application 选项卡中,保留 Output Type = Windows Application。
  2. 右键单击 App.xaml 并选择属性
  3. 设置 Build Action = Page
  4. 打开 App.xaml.cs 并修改 App 类,如下所示。

    public partial class App : Application
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool AttachConsole(uint dwProcessId);
    
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool FreeConsole();
    
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern int GetConsoleTitle(System.Text.StringBuilder sbTitle, int capacity);
    
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern bool SetConsoleTitle(string sTitle);
    
        [STAThread]
        public static int Main(string[] args)
        {
            Boolean hasExceptionOccured = false;
    
            System.Text.StringBuilder sbTitle = new System.Text.StringBuilder();
    
            try
            {
                // If the user does not provide any parameters assume they want to run in GUI mode.
                if (0 == args.Length)
                {
                    var application = new App();
                    application.InitializeComponent();
                    application.Run();
                }
                else
                {
                    const uint ATTACH_PARENT_PROCESS = 0x0ffffffff;  // Default value if not specifying a process ID.
    
                    // Attach to the console which launched this application.
                    AttachConsole(ATTACH_PARENT_PROCESS);
    
                    // Get the current title of the console window.
                    GetConsoleTitle(sbTitle, 64);
    
                    // Set the console title to the name and version of this application.
                    SetConsoleTitle(Global.thisProgramsName + " - v" + Global.thisProductVersion);
    
                    // Create a instance of your console class and call it’s Run() method.
                    var mainConsole = new ReportTester.MainConsole();
                    mainConsole.Run(args);                   
                }
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                System.Console.WriteLine(ex.StackTrace);
                if (null != ex.InnerException)
                {
                    System.Console.WriteLine(ex.InnerException.Message);
                    System.Console.WriteLine(ex.InnerException.StackTrace);
                }
                hasExceptionOccured = true;
            }
            finally
            {
                // Since the console does not display the prompt when freed, we will provide one here.
                System.Console.Write(">");
    
                // Restore the console title.
                SetConsoleTitle(sbTitle.ToString());
    
                // Free the console.
                FreeConsole();
            }
    
            return (hasExceptionOccured ? 1 : 0);
        }
    }
    
于 2015-04-10T23:48:18.090 回答
1

要求不明确。听起来好像唯一真正的要求是能够重定向标准输出;似乎不需要控制台窗口。

在空白(新)WPF 应用程序中,将以下内容添加到 Loaded 事件或其他内容中:

Stream StdoutStream = OpenStandardOutput();
StreamWriter Stdout = new StreamWriter(StdoutStream);
Stdout.WriteLine("Line one");
Stdout.WriteLine("Line two");
Stdout.WriteLine("Hello");
Stdout.WriteLine("Bye");
Stdout.Flush();
Stdout.Close();

然后从命令提示符处执行程序并将标准输出重定向到文件。输出将在文件中。标准输入可以以相应的方式重定向。

这对于标准 IO 是我们无法控制的要求的情况非常有用。我们可以有一个结合标准 IO 的 GUI 窗口。

于 2019-01-09T03:31:07.223 回答
0

如果您想为您的程序同时拥有一个控制台和一个 GUI 窗口,您可以通过将项目编译为控制台应用程序来实现这一点。

只需转到您的项目属性并将输出类型更改为console application

现在,当您运行时,您将同时获得 WPF 窗口和控制台窗口。

于 2019-04-26T02:42:34.140 回答