开发者

Why c# console app doesn't support redirection to file?

I created a classic console app

  开发者_运维技巧  static void Main(string[] args)
    {

It works except when I try to redirect output to a file

    myconsoleapp.exe arg1 arg2 arg3 > output.txt

Does C# console supports redirection ?


If you build the following class:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World! - {0}", args[0]);
    }
}

and execute

app.exe test > test.txt

you get the file test.txt with the content

Hello World! - test

If your code does different output writing please provide your methods of writing to the output.


Perhaps your messages are being sent to STDERR, not STDOUT. In that case, you need to redirect differently. From Microsoft's guidance ( http://support.microsoft.com/kb/110930 ):

You can print the errors and standard output to a single file by using the "&1" command to redirect the output for STDERR to STDOUT and then sending the output from STDOUT to a file:

dir file.xxx 1> output.msg 2>&1

Or, you can redirect the output to one place, and the errors to another.

dir file.xxx > output.msg 2> output.err


Without more information it is hard to figure out the problem. One thought I had was perhaps you are using System.Diagnostics.Debug.WriteLine to write to the debug console. In this case you would not be able to redirect output in the same way, at least not without some more work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜