Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run batch script before Debugging

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.

like image 848
Tilak Avatar asked Mar 15 '11 03:03

Tilak


People also ask

How do I run a batch script in debug mode?

By adding pause command: One way to debug batch file is by executing the pause command or operation and halting the executing program if any kind of error is found or occurred then a developer can easily fix the issue by restarting the process.

How do I run a batch file without displaying execution?

It'll run your batch file in invisible/hidden mode. Solution-2: If at all possible, modify the batch file to run whatever program with the start command. By default, start returns immediately without waiting for the program to exit, so the batch file will continue to run and, presumably, exit immediately.

Can you use debugger on batch file?

The debugger allows you to "single-step" through a batch file line by line, with the file displayed in a popup window as it executes. You can execute or skip the current line, continue execution with the debugger turned off, view the fully-expanded version of the command line, or exit the batch file.


2 Answers

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");
}
like image 187
Reddog Avatar answered Oct 05 '22 23:10

Reddog


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

like image 43
Vincent Hubert Avatar answered Oct 05 '22 22:10

Vincent Hubert