3

我有一个程序 test.exe,它接收参数并在我作为输入提供的特定 .txt 路径中写入一些日志。我想添加这个命令:

text.exe /r >> textFile.txt 

含义:构建项目并将日志写入 textFile.txt(不存在)我不确定它是如何工作的?是args[2] = ">>"args[3] = textFile.txt?我怎样才能使用它的名字??使困惑!

编辑:当我调试并查看本地人时,我只看到/r>> textFile.txt根本没有出现。如何从命令行获取它作为参数?

编辑2:

理解它应该自动重新定向,但是当我这样做时它只是失败并将我发送到一行:Console.CursorLeft = 0;这意味着当我没有控制台时我正在尝试用光标做一些事情......只是。文本文件

谢谢!

4

3 回答 3

7

不,命令外壳只会传递/rtest.exe,它会自动将输出重定向到附加到textFile.txt。您的应用程序看不到这一点 - 至少不是简单的。

如果您只是让您的程序写入标准输出(例如使用Console.WriteLine),那么输出将在textFile.txt.

于 2013-03-19T10:39:39.360 回答
2

命令提示符使用“>>”作为特殊命令,告诉它将输出重定向到以下文件。

你不需要在你的程序中处理这个;只需进行正常Console.WriteLine()呼叫,输出将被重定向。

于 2013-03-19T10:40:27.830 回答
0

When you run your application in command prompt and the pass parameters like:

c:\>test.exe /r >> textFile.txt

the command prompt understands that it need to pass only the parameter /r to our program the file name after >> is for his usage. e.g try running following command in your command prompt

c:\>ipconfig

and

c:\>ipconfig >> text.txt
c:\>notepad.exe text.txt

you will find for first time output is shown on screen and the second time it was written in file name you gave by command prompt itself.

于 2013-03-19T10:56:33.297 回答