views:

75

answers:

2

Hello,

I've got one quick question. Is there any simple way to detect if .net 2.0/3.x winforms application is run from visual studio (in any configuration debug/release/custom) ?

Best regards, Tomasz.

+2  A: 

I am not entirely sure about this, but I presume that Debugger.IsAttached would be true even in a release configuration.

Simon Svensson
This will be true for any application which is debugging your app, not just visual studio
JaredPar
My question wasn't too precise. This solution is quick and acceptable. Thanks.
tomo
+3  A: 

I'm not sure entirely what you mean. It seems like you could be asking one of the following

Determine if my application was launched for debugging from Visual Studio

For this case, as long as the hosting process is enabled, just look for vshost.exe to be the suffix of the process name.

var isVsDebugLaunched = Process.GetCurrentProcess().ProcessName.EndsWith("vshost.exe")

Determine if Visual Studio launched my process in any shape or form.

To accomplish this you will need to look at the parent process for the given process. I'm not sure there is a great way to do that in managed code via the BCL (still looking).

JaredPar
WMI is likely to be the easiest way to get the process ID. WIN32_Process has a ParentProcessId property.
Richard
I seem to recall it is possible to disable the use of the *vshost.exe approach?
Richard
@Richard, it is if you disable the hosting process or use a framework which does not support the hosting process.
JaredPar
Thanks for nice ideas.
tomo