0

I have an application that is being run as a post-build event:

Project Properties -> Build Events: call "$(TargetDir)\bin\myApp.exe" "$(ProjectPath)"

However, sometimes I will run it by itself - sometimes from Visual Studio and sometimes straight from the console.

How can I tell programmatically if an application is being run as a post/pre-build event or standalone (so sometimes there will still be debugger attached, but not always)?

Is there some kind of flag or environment variable I can check?

Note: I want to check this from within the application that is being run.

pushkin
  • 9,575
  • 15
  • 51
  • 95
  • What do you mean by running as a post/pre-build event? Do you mean is your app running with a debugger attached? Can you give a example? – Daniel Dec 21 '16 at 03:03
  • @Daniel Yes, I should mention that. I'm running with a debugger attached. I'm setting it up as a post or pre-build event through Project Properties -> Build Events. – pushkin Dec 21 '16 at 03:59
  • Can you please post your build event script? I think its unlikely that the debugger is attached **automatically** to a process started by a build event. – Daniel Dec 21 '16 at 04:49
  • I think this is an XY problem: *Why* do you want to know how your application was started? What difference could it possibly make? – abelenky Dec 21 '16 at 15:01
  • Could you add a parameter to your build event, and then check for it with GetCommandLineArgs. – WhoIsRich Dec 21 '16 at 15:01
  • 1
    @abelenky When I run as a post-build event, it outputs some stuff that is formatted in a way that Visual Studio understands, but is not particularly human-readable. If I run from the console, I want to format the output differently. – pushkin Dec 21 '16 at 15:26
  • @WhoIsRich I could add a parameter that tells it how it's being run, but I was wondering if there is another way of doing it. I'd prefer not passing anything else in. – pushkin Dec 21 '16 at 15:27

1 Answers1

-1

The following will tell you if the debugger is attached.

if(System.Diagnostics.Debugger.IsAttached)
{
    // ...
}
Daniel
  • 2,744
  • 1
  • 31
  • 41