Reputation: 5136
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.
Upvotes: 0
Views: 369
Reputation: 82267
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
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);
}
And it can be used like this
if (ConsoleEx.InputRedirected)
{
string strStdin = Console.In.ReadToEnd();
}
Upvotes: 1
Reputation: 1074
If the batch file knows the input then save the input to a file and feed that to your program like
prog.exe <argument.txt
in the batch file. I think you need not change the source code for this.
Upvotes: 1
Reputation: 20394
The batch file can set an environment variable and you can check that in your console application:
in the batch file:
set waitOnDone=0
yourExeFile -arguments
in your Console Application:
var env = System.Environment.GetEnvironmentVariable("waitOnDone");
if (String.IsNullOrEmpty(env) || env != "0")
{
// do something
}
Upvotes: 1
Reputation: 18832
Why not pass in a commandline argument to the console app to determine whether to quit immediately or wait.
Upvotes: 5