如何运行“.bat”文件安装过程中的文件?

发布于 2024-08-24 17:41:02 字数 132 浏览 1 评论 0原文

在安装项目中,可执行文件(例如“.exe 、 .dll 、 .js 、 .vbs”)是可接受的,但无法在自定义操作中运行 .bat 文件。

问题是如何在安装过程中运行*.bat文件?

In a Setup project the executable files such as ".exe , .dll , .js , .vbs" are acceptable but there is no way to run a .bat file in a Custom Action.

The question is how to run the *.bat files during installation?

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

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

发布评论

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

评论(4

煮茶煮酒煮时光 2024-08-31 17:41:02

好吧,经过大量搜索和反复试验,我解决了这个问题。我不确定这是否是最好的方法,但它确实有效。

场景如下:我有一个应用程序想要通过 Visual Studio 安装项目进行部署。除了我的应用程序文件之外,我还想在目标目录中创建一个包含批处理 (.bat) 文件的子目录。我希望该文件在安装过程结束时运行。

您要做的如下:

  1. 创建一个安装项目并像平常一样进行配置,包括放置批处理文件的子目录(如果您不想将其放在子目录中,可以直接将其放置在应用程序文件夹中) 。
  2. 在“文件系统”视图中(在解决方案资源管理器中右键单击项目 -> 查看 -> 文件系统),添加要执行的批处理文件 cmd.exe(C: \Windows\System32\cmd.exe)
  3. 打开“自定义操作”视图(在解决方案资源管理器中右键单击项目->视图->自定义操作)
  4. 右键单击“提交”并选择“添加自定义操作”
  5. 导航到 cmd.exe 并选择它。
  6. 打开新创建的自定义操作的属性面板。
  7. 从 Arguments 属性中删除 /Commit。
  8. 在 Arguments 属性中输入:/c "[TARGETDIR]subdirectoryname\batchfile.bat",其中 subdirectoryname 应替换为您的子目录的名称(如果您将批处理像我一样将文件放在子目录中...如果没有,该值应该是 /c "[TARGETDIR]batchfile.bat") ,而 batchfile.bat 应该是批处理文件的文件名。

就是这样。一旦安装过程的其余部分完成,现在将执行批处理文件。

为了清楚起见,这里有一个示例:

我的批处理文件:blah.bat
我的子目录:mydir

针对 cmd.exe 的自定义操作的参数值将为

/c "[TARGETDIR]mydir\blah.bat"

Well, after much searching and trial and error I have solved this. I'm not sure if this is the best way, but it works.

Here's the scenario: I have an application I would like to deploy via a Visual Studio Setup project. In addition to my application files, I would like to create a subdirectory in the target directory that contains a batch (.bat) file. I would like this file to run at the end of the installation process.

Here's what you do:

  1. Create a setup project and configure as you normally would, including the subdirectory in which you'll place your batch file (you can just place it in the Application Folder directly if you don't want it in a subdirectory).
  2. In the "File System" view (right-click on the project in Solution Explorer->View->File System), add the batch file you want to execute and cmd.exe (C:\Windows\System32\cmd.exe)
  3. Open the "Custom Actions" view (right-click on the project in Solution Explorer->View->Custom Actions)
  4. Right-click on "Commit" and choose "Add Custom Action"
  5. Navigate to and select cmd.exe.
  6. Open the properties panel for the newly created custom action.
  7. Delete /Commit from the Arguments property.
  8. Enter: /c "[TARGETDIR]subdirectoryname\batchfile.bat" in the Arguments property, where subdirectoryname should be replaced by the name of your subdirectory (if you put the batch file in a subdirectory like I did... if you didn't, the value should be /c "[TARGETDIR]batchfile.bat") and batchfile.bat should be the filename of your batch file.

That's it. The batch file will now be executed once the rest of the installation process is completed.

Here's an example for the sake of clarity:

My batch file: blah.bat
My subdirectory: mydir

The value of the Arguments for my custom action targeting cmd.exe would then be

/c "[TARGETDIR]mydir\blah.bat"

锦爱 2024-08-31 17:41:02

达到相同结果的另一种方法是将 .vbs 文件放入运行相应 .bat 文件的自定义操作中。
以下代码是我放入安装应用程序文件夹中的“RunRegisterComponents.vbs”。当然,我将 [TARGETDIR] 作为 .vbs 参数放在 Visual Studio 属性窗口中。

dim WshShell
Set WshShell = CreateObject("WScript.Shell")

' Read the "CustomActionData" property holding the install directory.
dim programDir
programDir=  property("CustomActionData")

' Make the batch full file name and parameter
commandString = chr(34) & programDir & "RegisterComponents.bat" & chr(34) & " " & chr(34) &
programDir& chr(34)

' Set the current directory
WshShell.CurrentDirectory = programDir

' Run batch.
ret = WshShell.Run (commandString, 0, 0)

这就是我设置自定义操作的方式:

在此处输入图像描述

我希望这可以帮助您!

One other way to reach the same result is put a .vbs file in custom actions that runs the correspondent .bat file.
The following code is the "RunRegisterComponents.vbs" I put in setup application folder. Of course I put [TARGETDIR] as .vbs parameter in Visual Studio property window.

dim WshShell
Set WshShell = CreateObject("WScript.Shell")

' Read the "CustomActionData" property holding the install directory.
dim programDir
programDir=  property("CustomActionData")

' Make the batch full file name and parameter
commandString = chr(34) & programDir & "RegisterComponents.bat" & chr(34) & " " & chr(34) &
programDir& chr(34)

' Set the current directory
WshShell.CurrentDirectory = programDir

' Run batch.
ret = WshShell.Run (commandString, 0, 0)

That is as I set my custom actions:

enter image description here

I hope this can help you!

不念旧人 2024-08-31 17:41:02
  1. 查看这篇文章(文章已弃用),尽管它是在 VB.NET 中,但它也适用于 C#。最重要的部分是(翻译为 C#)创建一个新的类库,并添加一个包含以下内容的新安装程序类:如文章中所述,然后您可以引用刚刚创建的项目创建一个新的自定义操作。< /p>

    覆盖 void Commit(IDictionary savingState)
    {
         base.Commit(savedState);
         System.Diagnostics.Process.Start("myApp.bat","你的bat参数");
    }
    
  2. 现在我们正在将批处理文件添加到您的安装程序项目中。创建一个安装项目并像平常一样进行配置,包括放置批处理文件的子目录(如果您不想将其放在子目录中,可以直接将其放置在应用程序文件夹中)。

  3. 在“文件系统”视图中(在解决方案资源管理器中右键单击项目->视图->文件系统),添加要执行的批处理文件。

  4. 构建安装程序项目。

  1. Check this article (article is deprecated), even though it is in VB.NET it applies to C# as well. The most important part is (translated to C#) creating a new Class Library, and adding a new Installer Class with the following content: As stated in the article you can then create a new custom action with a reference to your just created project.

    override void Commit(IDictionary savedState)
    {
         base.Commit(savedState);
         System.Diagnostics.Process.Start("myApp.bat","your bat arguments");
    }
    
  2. Now we are adding batch file to your installer project. Create a setup project and configure as you normally would, including the subdirectory in which you'll place your batch file (you can just place it in the Application Folder directly if you don't want it in a subdirectory).

  3. In the "File System" view (right-click on the project in Solution Explorer->View->File System), add the batch file you want to execute.

  4. Build the installer project.

墨落成白 2024-08-31 17:41:02

如果您尝试在安装过程中运行具有相对路径的批处理文件,那肯定会失败。这是因为批处理文件将考虑安装程序运行的目录,而不是安装文件的目录。使用安装程序构建器将批处理文件复制到临时目录中。

If you are trying to run a batch file that have relative paths during the installation process, that will fail for sure. That's because the batch file will take into account the directory where the installer is running, and not where the files were being installed. Use installer builders that copies batch files into temporary directory.

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