如何运行“.bat”文件安装过程中的文件?
在安装项目中,可执行文件(例如“.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,经过大量搜索和反复试验,我解决了这个问题。我不确定这是否是最好的方法,但它确实有效。
场景如下:我有一个应用程序想要通过 Visual Studio 安装项目进行部署。除了我的应用程序文件之外,我还想在目标目录中创建一个包含批处理 (.bat) 文件的子目录。我希望该文件在安装过程结束时运行。
您要做的如下:
/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:
/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"
达到相同结果的另一种方法是将 .vbs 文件放入运行相应 .bat 文件的自定义操作中。
以下代码是我放入安装应用程序文件夹中的“RunRegisterComponents.vbs”。当然,我将 [TARGETDIR] 作为 .vbs 参数放在 Visual Studio 属性窗口中。
这就是我设置自定义操作的方式:
我希望这可以帮助您!
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.
That is as I set my custom actions:
I hope this can help you!
查看这篇文章(文章已弃用),尽管它是在 VB.NET 中,但它也适用于 C#。最重要的部分是(翻译为 C#)创建一个新的类库,并添加一个包含以下内容的新安装程序类:如文章中所述,然后您可以引用刚刚创建的项目创建一个新的自定义操作。< /p>
现在我们正在将批处理文件添加到您的安装程序项目中。创建一个安装项目并像平常一样进行配置,包括放置批处理文件的子目录(如果您不想将其放在子目录中,可以直接将其放置在应用程序文件夹中)。
在“文件系统”视图中(在解决方案资源管理器中右键单击项目->视图->文件系统),添加要执行的批处理文件。
构建安装程序项目。
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.
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).
In the "File System" view (right-click on the project in Solution Explorer->View->File System), add the batch file you want to execute.
Build the installer project.
如果您尝试在安装过程中运行具有相对路径的批处理文件,那肯定会失败。这是因为批处理文件将考虑安装程序运行的目录,而不是安装文件的目录。使用安装程序构建器将批处理文件复制到临时目录中。
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.