How to invoke VS Debugger on C# program from command line (devenv.exe usage?)

مؤمّن How to invoke VS Debugger on C# program from command line (devenv.exe usage?)

  • Thursday, January 26, 2012 4:28 AM
     
     

    How does one invoke the Visual Studio debugger from a command prompt on a C# application that has been previously built in Visual Studio 2010 for debug - without first loading the project up in Visual Studio?

     

    Some internet forums claim this can be done for VC++ but not for VB.net or C#.

All Replies

  • Thursday, January 26, 2012 2:46 PM
    Moderator
     
     

    That is correct.  The VS command line has a special option (DebugExe) to allow you to begin debugging native applications automatically.

    You can get the VS debugger to automatically start whenever you run an EXE via the registry (http://msdn.microsoft.com/en-us/library/a329t4ed.aspx) but this is generally only done while debugging code that cannot otherwise be debugged. 

    You can get VS to load, compile and run a solution at startup via the /Run command but this isn't quite what you asked for.

    I'm more interested in knowing what problem you're trying to solve?  Why would you ever need to start VS and begin debugging without the project being loaded?  Technically VS always loads a solution (even with native EXEs) but it'll create one if it needs it.  But unless you load the project the debugging experience is lackluster at best.  It is hard to step through code, you don't have any breakpoints and even the contextual information around variables isn't available in many cases.

    Note that you can trigger the VS debugger to start when you run an EXE you have control over by either throwing an explicit exception at startup or using Debug.Assert.  Both cases allow the debugger to attach to the process to give you what you want.  Of course you generally wouldn't have this in the final version.

    Michael Taylor - 1/26/2012
    http://msmvps.com/blogs/p3net

  • Tuesday, January 31, 2012 7:44 PM
     
     Answered
    Hi hhinman2,
    Try using System.Diagnostics.Debugger.Launch in your application's entry point (eg. Main).
    • Proposed As Answer by Dave_Anderson Wednesday, February 01, 2012 12:09 AM
    • Marked As Answer by hhinman2 Wednesday, February 01, 2012 12:31 AM
    •  
  • Tuesday, January 31, 2012 8:12 PM
     
     

    Thanks Dave, I figured this out and indeed I am using System.Diagnostics.Debugger.Launch.

    For Michael, I am writing something rather advanced where users will write programs that run under my layer of code. I don't want them seeing or debugging my layer so I need to be able to fire the debugger for the user's code so they can debug.