Run batch script before Debugging

主宰稳场 提交于 2019-12-04 10:41:46

问题


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.


回答1:


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



回答2:


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




回答3:


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

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




回答4:


To run script before debugging with "Pre-Build Events":

Add your command line in the "Properties" of your projet > "Build Events":

cmd /c i:\foo.bat

A good example here => https://stackoverflow.com/a/57570301/2736742




回答5:


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


来源:https://stackoverflow.com/questions/5307309/run-batch-script-before-debugging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!