控制台应用程序可以知道它是从批处理文件中调用的吗?
有没有办法让控制台应用程序知道它是否是从批处理文件中调用的,而不是直接在命令提示符下调用的?
询问的原因是找到一种方法来决定是否启动 Console.ReadLine 循环或类似的等待进一步输入,或者是否立即退出。
或者,有没有办法让批处理文件继续将输入发送到正在等待通过 ReadLine 进一步输入的控制台应用程序?
是的,我知道 - 这是两个问题。如果有人评论说第二个问题有答案,我会单独问。
Is there a way for a Console app know whether it has been called from a batch file as opposed to directly at the command prompt?
The reason for asking is to find a way to decide whether to initiate a Console.ReadLine loop or similar to await further input, or whether to exit immediately.
Alternatively, is there a way for a batch file to continue sending input to a Console App that is awaiting further input via ReadLine?
Yes, I know - that's 2 questions. If anyone comments that there's an answer to the second question I'll ask that separately.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不将命令行参数传递给控制台应用程序来确定是否立即退出或等待。
Why not pass in a commandline argument to the console app to determine whether to quit immediately or wait.
批处理文件可以设置环境变量,您可以在控制台应用程序中检查:
在批处理文件中:
在控制台应用程序中:
The batch file can set an environment variable and you can check that in your console application:
in the batch file:
in your Console Application:
如果批处理文件知道输入,则将输入保存到文件中,并将其提供给您的程序,就像
在批处理文件中一样。我认为你不需要为此更改源代码。
If the batch file knows the input then save the input to a file and feed that to your program like
in the batch file. I think you need not change the source code for this.
如果有重定向(从批处理文件),您的问题可能是仅从标准输入读取。
这也可以通过检测是否有输入流来解决(使用 dotnet)。
来自 @Hans Passant 的解决方案,SO:如何检测标准输入中的控制台是否已被重定向重定向
并且可以这样使用
Possibly your problem is to read only from stdin if there is a redirecton (from your batch file).
This can also be solved (with dotnet) by detecting if there is an input stream.
Solution from @Hans Passant, SO: how to detect if console in stdin has been redirected
And it can be used like this