开发者

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 e开发者_Python百科vents.

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.


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");
}


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


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.


if $(ConfigurationName) == Debug mkdir c:\mydir

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜