结束控制台应用程序 - 未显示文件路径

时间:2014-08-20 22:09:10

标签: c# .net wpf console

我正在尝试创建一个也可以从命令行运行的WPF应用程序。我有一个WPF应用程序,我能够在某种程度上实现这一点。可以使用0参数运行,并且将加载一个窗口。

当我用>运行它时0个参数的想法是让它从命令行运行,因此应该打印到命令行。所以为此,我有一个控制台Attached,然后我完成了Freed控制台。

但是,一切都完成后,命令行会产生以下结果。

  

能够在这里运行另一个命令,但为什么不显示完整目录以明确它已经完成?

     

无论如何这可以解决吗?

Command line output

在进一步检查时,我注意到它似乎是在输出之前而不是之后打印新行。

enter image description here

public partial class App : Application
{
    [DllImport("Kernel32.dll")]
    public static extern bool AttachConsole(int processId);

    [DllImport("Kernel32.dll")]
    private static extern bool FreeConsole();

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        if (e.Args.Length > 0)
        {
            AttachConsole(-1);
            Console.WriteLine("\nStart");
            Console.WriteLine("Stop");
            Console.WriteLine("");
            FreeConsole();
        }
        else
        {
            new MainWindow().ShowDialog();
        }
        this.Shutdown();
    }
}

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用AllocConsole功能?我总是毫无问题地使用它。

[DllImport("Kernel32")]
internal static extern bool AllocConsole();