0

I have a console application in which I need to detect whether or not it was executed from a command line, as opposed to from within a batch file. I want to protect

            Console.Write ( "Press any key to exit" );
            Console.ReadKey ( true );
            Console.WriteLine ( );

from executing when the app is executing from within a batch file.

Gus
  • 1,383
  • 2
  • 12
  • 23
  • 2
    I'm not sure if there's a reliable way to do that. A more standard approach would be for the console application to accept a command-line argument which puts it into a "silent" or a "non-interactive" mode of some sort. Which you would use as a flag to prompt the user for input or continue with default inputs. – David Feb 11 '13 at 17:57
  • unfortunately I do not control from whence the app will be executed: command line or batch file – Gus Feb 11 '13 at 19:27
  • But do you control the app? You would simply add it as an option to the app itself. It would be the responsibility of whoever executes it to supply the necessary options. – David Feb 11 '13 at 19:33
  • Found an answer[^]. As usual thanks Hans. [^]: http://stackoverflow.com/questions/3453220/ – Gus Feb 11 '13 at 19:34

1 Answers1

6

You can do crazy things (like get the parent process, see what arguments it got etc), but if you control the app just add a command line argument, call it "batch mode" or whatever and when program is invoked with it make it non-interactive.

MK.
  • 33,605
  • 18
  • 74
  • 111
  • unfortunately I do not control from whence the app will be executed: command line or batch file. – Gus Feb 11 '13 at 19:26