Blog of me, Chris Idzerda, containing my thoughts, comments, and questions. RSS Feed


Detect Visual Studio Debugging

If you have code that you want to run only while debugging in Visual Studio, you can detect it if you are running in its hosting process, which is enabled by default.

#if DEBUG
    if(AppDomain.CurrentDomain.FriendlyName.EndsWith("vshost.exe"))
    {
        // Perform whatever tasks depend on running inside the Visual
        // Studio debugger.
    }
#endif

While it does provide several features not otherwise available, you might have a reason to disable it (such as a corrupted vshost.exe file). To do so, uncheck the last box (shown checked here) on the Debug tab in the Project Properties window.

Project Properties - Debug
 
Posted by Chris Idzerda | 3 Comments | Trackback Url | Bookmark with:        
Tags:

Links to this Post

Comments

Thursday, 5 Jun 2008 07:49 by Even better solution
if (System.Diagnostics.Debugger.IsAttached) { // perform tasks } This way you don't have to worry about the code not working if you aren't using the vshost process.

Monday, 9 Jun 2008 12:13 by You can also use ...
System.Diagnostics.Debugger.IsAttached Although it doesn't actually guarantee that debugger is VS.

Wednesday, 11 Jun 2008 09:35 by RE: other solutions
In the scenario that motivated this post, my intent was specifically to detect Visual Studio, although it's possible other debuggers use a similar mechanism. (I don't know of any.) Using the application's friendly name seemed less onerous than looking for the process and we didn't need to worry about not using the hosting process.

Name:
URL:
Email:
Comments:

CAPTCHA Image Validation