1

I need to use Visual Studio to build a .msi file that will execute some commands I have in a .bat with a custom action. I know I can accomplish this by going into the .msi and altering the tables and I've gotten that to work, but it's just a short fix and I need to get this working. In particular I want to execute my custom action on the uninstall.

First thing I tried was writing a .exe that called my .bat. It works stand-alone, but whenever I put the files in the File System and have the custom action run the .exe it just doesn't work. Who knows why..

The other thing I did was have the custom action use cmd.exe with my .bat as an argument. I get an error during the uninstall and the event viewer narrows it down to cmd.exe using /c "[TARGETDIR]batfile.bat very similarly to https://stackoverflow.com/a/6286046/1427105.

Any thoughts that can help me out?

Community
  • 1
  • 1
ReddShepherd
  • 467
  • 1
  • 11
  • 24
  • What is the error message from cmd? Or does it not start at all? You can change `/c` to `/k` for testing: with `/k` it executes the batch and then stays interactive. Type `exit` to close the session. – Alexey Ivanov Jun 05 '12 at 06:18
  • It could fail because you are trying to launch the BAT file after it was removed by the uninstall process. Have you checked that? – Bogdan Mitrache Jun 05 '12 at 13:52
  • The error is: 'C:\Program' is not recognized as an internal or external command, operable program or batch file. My batch file is: cd "C:\inetpub" pause I'm going to have a different batch file later obviously, but this is what I'm doing until I can get it working. And inetpub does exist on this machine. And if I run cmd.exe in my project and run test.bat it works fine. It's only through this uninstall.. – ReddShepherd Jun 05 '12 at 14:19
  • Yeah I've checked that the custom action is happening before they get removed. – ReddShepherd Jun 05 '12 at 14:29

1 Answers1

2

What about something like this?

protected override void OnBeforeUninstall(IDictionary savedState)
{
    base.OnBeforeUninstall(savedState);

    string assemblyPath = Context.Parameters["assemblypath"];
    string parentDirectory = Directory.GetParent(assemblyPath).FullName;
    string batch = Path.Combine(parentDirectory, "batfile.bat");
    Process p = Process.Start(batch);

    while (!p.HasExited)
        Thread.Sleep(1000);
}
Nick Spreitzer
  • 10,242
  • 4
  • 35
  • 58
  • This worked! For anyone who needs more help understanding how to make this work: 1. Create a .dll 2. Use this code 3. Put the .dll in your file system 4. Create a custom action using this file – ReddShepherd Jun 05 '12 at 14:47