none
Need code to check weather console application is triggered as standalone or called by any other application RRS feed

  • Question

  • Hi,

    I have a console application which will run in background(No UI). Now, I need code to check weather this console application is triggered as standalone or called by any other application. I am programming on VB.net language.

    Monday, May 14, 2018 4:00 PM

All replies

  • You will need to add a command line argument to your Console application so that it can determine whether it was launched from another app. The Console application can then add this information (or flag) to the Registry section for the app, or write it to a log file or database that can be queried.

    There is no native method that would indicate whether the Console application was run directly by a user or by an application.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Proposed as answer by Cor Ligthert Monday, May 14, 2018 5:52 PM
    • Unproposed as answer by Cor Ligthert Monday, May 14, 2018 5:52 PM
    Monday, May 14, 2018 4:18 PM
  • Check the parent pid (for example with ParentProcessId

    from Win32_Process class )

    Monday, May 14, 2018 4:20 PM
  • hi Paul

    saw this article but didn't understand.. Would you please elaborate bit on how to implement? Thanks in advance.

    Monday, May 14, 2018 4:44 PM
  • I saw this article but didn't understand.. Would you please elaborate bit on how to implement? Thanks in advance.

    What article? Are you referring to GetCommandLineArgs? Essentially the app that launches the console app would pass a command line argument that indicates it is being started from a another process. For example you use something like:

    MyConsoleApp.exe noninteractive

    ...where "noninteractive" on the command line would mean that the app was not run by a user. You could use Environment.GetCommandLineArgs in the Console app to look for this value when it launches in order to determine whether it was run by another app. If it was run from another app you can log this information as I mentioned.

    https://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs(v=vs.110).aspx

    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Proposed as answer by Cor Ligthert Monday, May 14, 2018 5:53 PM
    Monday, May 14, 2018 4:55 PM
  • [...] I need code to check weather this console application is triggered as standalone or called by any other application. 


    Do you mean other applications that are made by you too?
    Monday, May 14, 2018 5:58 PM
  • See this answer on StackOverflow.

    Here is the VB version of the code posted there:

    Module Module1
        Private StartedDirectly As Boolean = Not Console.IsOutputRedirected AndAlso
            Not Console.IsInputRedirected AndAlso
            Not Console.IsErrorRedirected AndAlso
            Environment.UserInteractive AndAlso
            Environment.CurrentDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) AndAlso
            Console.CursorTop = 0 AndAlso Console.CursorLeft = 0 AndAlso
            Console.Title = Environment.GetCommandLineArgs()(0) AndAlso
            Environment.GetCommandLineArgs()(0) = System.Reflection.Assembly.GetEntryAssembly().Location
    
        Sub Main()
            Console.WriteLine("Started Directly = " & StartedDirectly.ToString)
            Console.ReadKey()
        End Sub
    End Module

    StartedDirectly will be True if the EXE was double-clicked or run from Visual Studio.  It will be false if started from within a command prompt or by another application (e.g. using Process.Start).

    Note there is another comment in the StackOverflow thread that changes the behavior for launching from VS.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, May 14, 2018 8:52 PM
    Moderator