如何在安装完成后运行批处理脚本?

7

我正在为一个使用Visual Studio 2008(安装和部署>安装项目)开发的自定义安装程序工作,该安装程序是为C#项目开发的。我想在安装完成后运行一个批处理文件(*.bat),请问我该怎么做?


我也很好奇为什么VS安装程序只允许我使用dll、exe、js和vbs文件。 - cnd
2个回答

6
你需要扩展Installer类并重写Committed事件。
这里有一个示例。希望你能找到如何在C#中运行.bat文件的方法。
[RunInstaller(true)]
public class ServiceInstaller : Installer
{
    string strServiceName = "MyServiceName";

    public ServiceInstaller()
    {
        // .............

        this.Committed += new InstallEventHandler(ServiceInstaller_Committed);
    }

    void ServiceInstaller_Committed(object sender, InstallEventArgs e)
    {
        // Run your batch file
    }
}

自定义安装操作是另一个选项。这里有一个类似的讨论。


2

您可以使用cmd.exe来运行批处理文件,实际上它就是执行批处理文件的工具。

按照以下方式启动:cmd.exe /c <path-to-batch>\batchfile.bat


在2010年的设置中,无法更改cmd.exe的源路径。当XP中cmd.exe的位置位于c:\winnt\system32时,此操作会失败,但在Windows 7中它位于c:\windows\system32。我被难住了。 - pithhelmet
@pithhelmet %comspec% 环境变量存储了 cmd.exe 的完整路径。 %windir% 指向 Windows 目录,因此 %windir%\system32\cmd.exe 正确指向 cmd.exe。即使 Windows 不在 C: 驱动器上,它也可以正常工作。由于 system32PATH 环境变量中列出,您可以省略对 cmd.exe 的完整路径。 - Alexey Ivanov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接