在 VB.net 中写入控制台和标准输出

发布于 2024-09-11 10:53:55 字数 372 浏览 7 评论 0原文

我有一个正在写入控制台的 winform 应用程序,它似乎运行良好。我正在使用这段代码:

AttachConsole(-1)
 Console.Out.WriteLine("Hellow world")
  FreeConsole()

问题是: 如果我从命令行运行应用程序的 exe 文件,并尝试将输出重定向到文件中。这不起作用。 例如:

C:\ > myapp.exe > c:\output.txt

我仍然将输出输出到控制台屏幕(c:\output.txt 文件已创建但为空),但我希望将其保存到 c:\output.txt 出了什么问题?怎么做呢?

非常感谢!

I have a winform app that is writing to console and it seems to work well. I'm using this code:

AttachConsole(-1)
 Console.Out.WriteLine("Hellow world")
  FreeConsole()

The question is:
If I run the app's exe file from command line, and try to redirect the output into a file. It doesn't work.
For example:

C:\ > myapp.exe > c:\output.txt

I still get the output to console screen (c:\output.txt file is created but empty), but I want it to to be saved into c:\output.txt
What's going wrong ? How to do that?

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

脸赞 2024-09-18 10:53:55

如果您首先检查输出是否被重定向,那么您可以鱼与熊掌兼得。这是一个包含 P/Invoke voodoo 的小帮助器类:

using System;
using System.Runtime.InteropServices;

public static class ConsoleEx {
    public static bool OutputRedirected {
        get { return FileType.Char != GetFileType(GetStdHandle(StdHandle.Stdout)); }
    }
    public static bool InputRedirected {
        get { return FileType.Char != GetFileType(GetStdHandle(StdHandle.Stdin)); }
    }
    public static bool ErrorRedirected {
        get { return FileType.Char != GetFileType(GetStdHandle(StdHandle.Stderr)); }
    }

    // P/Invoke:
    private enum FileType { Unknown, Disk, Char, Pipe };
    private enum StdHandle { Stdin = -10, Stdout = -11, Stderr = -12 };
    [DllImport("kernel32.dll")]
    private static extern FileType GetFileType(IntPtr hdl);
    [DllImport("kernel32.dll")]
    private static extern IntPtr GetStdHandle(StdHandle std);
}

用法:

bool redir = ConsoleEx.OutputRedirected;
if (!redir) AttachConsole(-1);
// etc...

You can have your cake and eat it too if you first check if output was redirected. Here's a little helper class that contains the P/Invoke voodoo:

using System;
using System.Runtime.InteropServices;

public static class ConsoleEx {
    public static bool OutputRedirected {
        get { return FileType.Char != GetFileType(GetStdHandle(StdHandle.Stdout)); }
    }
    public static bool InputRedirected {
        get { return FileType.Char != GetFileType(GetStdHandle(StdHandle.Stdin)); }
    }
    public static bool ErrorRedirected {
        get { return FileType.Char != GetFileType(GetStdHandle(StdHandle.Stderr)); }
    }

    // P/Invoke:
    private enum FileType { Unknown, Disk, Char, Pipe };
    private enum StdHandle { Stdin = -10, Stdout = -11, Stderr = -12 };
    [DllImport("kernel32.dll")]
    private static extern FileType GetFileType(IntPtr hdl);
    [DllImport("kernel32.dll")]
    private static extern IntPtr GetStdHandle(StdHandle std);
}

Usage:

bool redir = ConsoleEx.OutputRedirected;
if (!redir) AttachConsole(-1);
// etc...
蓬勃野心 2024-09-18 10:53:55

您正在附加到父进程以提供输出,在您的情况下可能是 cmd.exe。父进程的 stdout 流尚未重定向,因此继续在屏幕上显示输出。

我不知道有直接的方法。如果您不调用 AttachConsole,您会发现重定向按预期工作,但当然您会失去拥有控制台窗口的选项。然而,有一个我认为合理的解决方法。

如果您希望输出转到控制台窗口,那么您可以为应用程序提供一个指示此要求的命令行开关,例如

C:\> myapp.exe /console

/console 参数存在时,您可以调用 AttachConsole 并且输出将被写入控制台。当此开关不存在时,您不会调用 AttachConsole 并且您将能够将输出重定向到文件。

You are attaching to the parent process to provide output, which in your case is probably cmd.exe. The parent process' stdout stream has not been redirected and therefore continues to display the output on the screen.

I am not aware of a direct approach. If you do not call AttachConsole you will find that the redirect works as expected, but of course then you loose the option to have a console window. However, there is a work around that I think is reasonable.

If you want the output to go to a console window then you provide your application with a commandline switch that indicates this requirement, something like

C:\> myapp.exe /console

When the /console argument is present you call AttachConsole and the output will be written to the console. When this switch is not present you do not make the call to AttachConsole and you will be able to redirect the output to a file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文