调试前运行批处理脚本

发布于 2024-10-22 13:32:43 字数 208 浏览 2 评论 0原文

我想每次在启动程序进行调试之前运行批处理脚本。

对于构建事件,此类功能是使用预构建事件、构建后事件来实现的。

对于实际调试,我找不到任何预调试、调试后事件。

如何实现这个场景呢?

我正在使用 VS2008、.net Framework 3.5、C# 应用程序。

我反对在应用程序中创建一些额外的代码行来启动外部批处理文件。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尸血腥色 2024-10-29 13:32:43

我知道您希望避免额外的代码,但在您的 Main 函数中,您可以使用 Debugger.IsAttached() 为您开始工作。

例如:

if (Debugger.IsAttached)
{
     System.Diagnostics.Process.Start(@"C:\myBatchFile.bat");
}

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");
}
少女的英雄梦 2024-10-29 13:32:43

您可以使用 VS 宏。

我遇到了同样的问题,这是迄今为止我遇到的最好的问题

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

对于如何向宏添加事件处理程序有一个很好的解释
此处

到目前为止,我遇到的唯一问题是弄清楚如何获取当前调试的应用程序活动目录

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

°如果伤别离去 2024-10-29 13:32:43

对我有用的基本解决方案(在 VS 2017 中)是创建一个批处理文件,该文件执行应在调试之前运行的命令,并且还包括作为最后一行的一些通过命令行传递的参数,例如现在,

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

在调试属性中,将“命令”设置为批处理文件,对于“命令参数”,包括 $(TargetPath) 作为第一个参数,后跟程序使用或需要的任何参数:

$(TargetPath) my command args

YMMV,但对于我的简单需要这似乎运作良好。

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.

゛清羽墨安 2024-10-29 13:32:43
if $(ConfigurationName) == Debug mkdir c:\mydir

您应该查看... 如何仅针对调试构建运行 Visual Studio 构建后事件

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

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

逆光飞翔i 2024-10-29 13:32:43

那么,您有一个想要通过预构建事件运行的 .bat 文件吗?
尝试在预构建事件命令中指定批处理文件的完整路径,例如

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

cmd C:\windows\system32\cmd.exe /c C:\Path\to\foo.bat

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文