13

I want to run a batch script every time before starting program for debugging.

For the build events, such functionality is realized using pre-build event, post-build event.

For actual debugging, I could not find any pre-Debug, post-Debug events.

How to realize this scenario?

I am using VS2008, .net framework 3.5, c# application.

I am opposed to idea of creating some extra lines of code within the application that would fire-up external batch file.

Tilak
  • 30,108
  • 19
  • 83
  • 131

5 Answers5

14

I realise you wished to avoid additional code, but in your Main function you could use Debugger.IsAttached() to kick off your work for you.

For example:

if (Debugger.IsAttached)
{
     System.Diagnostics.Process.Start(@"C:\myBatchFile.bat");
}
Doc Brown
  • 19,739
  • 7
  • 52
  • 88
Reddog
  • 15,219
  • 3
  • 51
  • 63
  • 2
    A slightly cleaner way of doing this would be to create a separate project that calls `System.Diagnostics.Process.Start(@"C:\myBatchFile.bat");`, then run multiple startup projects with your new project calling the batch file. Then you avoid changing your production code. – zola25 Apr 20 '19 at 13:18
4

You can use a VS macro.

I had the same issue and this is the best I came with so far

Dim MustUpdateDB As Boolean

    Private Sub DebuggerEvents_OnEnterRunMode(ByVal Reason As EnvDTE.dbgEventReason) Handles DebuggerEvents.OnEnterRunMode
        If (MustUpdateDB) Then
            MsgBox("Start debug operation", MsgBoxStyle.OkOnly, "TITLE")
            REM DO WHATEVER COMMAND HERE
            REM  System.Diagnostics.Process.Start("C:\listfiles.bat")
            MustUpdateDB = False
        End If


    End Sub

    Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone
        MsgBox("Build Done", MsgBoxStyle.OkOnly, "Title")
        MustUpdateDB = True
    End Sub

There is a pretty good explanation on how to add event handlers to a macro here

The only issue I have so far is to figure out how to get the currently debugged application active directory

Vincent Hubert
  • 1,386
  • 9
  • 23
0

A basic solution that worked for me (in VS 2017) was to create a batch file that does the command(s) that should run before debugging, and also include as the last line some parameters to be passed via command-line, such as this:

rem Place the command(s) you need here:
xcopy pristine.file changed.file

rem Now process passed commands - a few extra placeholders shouldn't hurt anything, to
rem allow for some extra command-line parameters
%1 %2 %3 %4 %5 %6 %7

Now in the debugging properties, set the 'Command' to your batch file, and for 'Command Arguments' include $(TargetPath) as the first argument, followed by any arguments your program uses or needs:

$(TargetPath) my command args

YMMV, but for my simple needs this seems to be working well.

MathedMan
  • 1
  • 1
-2
if $(ConfigurationName) == Debug mkdir c:\mydir

You should check out... How to run Visual Studio post-build events for debug build only

Community
  • 1
  • 1
dotalchemy
  • 2,459
  • 19
  • 24
  • 2
    Actually I need something that is fired just before debug, not after post build event. I have to run a script every time the tool is launched in debug mode. Even though there is no change in source code (and hence no build, only tool launch). – Tilak Mar 23 '11 at 17:16
  • @Tilak, were you able to find some solution for this? – Infinity Dec 16 '22 at 09:20
-4

So, you have a .bat file that you want to run via the pre-build event? Try to specify full path to your batch file in the pre-build event command e.g.

cmd /c C:\Path\to\foo.bat

or

cmd C:\windows\system32\cmd.exe /c C:\Path\to\foo.bat
Thracian
  • 651
  • 4
  • 8
  • 24
  • 2
    You have probably completely misunderstood the question.. Just keep in mind that "Pre/Post-build" events doesn't run before EVERY debugging start. For ex, if you haven't made any change to your code after last build, the build wont be triggered before debug. – Zé Carlos Jan 20 '16 at 14:51