How to extract and run a file during installation

2.2k views Asked by At

I have created a Custom Action (DTF) with C#.
In that CA, I would like to extract a file from the msi (declared as Binary in wix) and run it with some arguments.
I haven't found any samples or help about that..
I have to execute a request on the msi, but I would like to have a sample. Thanks!

1

There are 1 answers

0
Yan Sklyarenko On BEST ANSWER

The DTF.chm has a sample how to update the Binary table. It's in "Working with MSI Databases" topic. And you can guess how to do the opposite operation. The code might look like this:

  using (var db = new Database("test.msi", DatabaseOpenMode.Direct))
  {
    using (var view = db.OpenView("SELECT `Data` FROM `Binary` WHERE `Name` = '{0}'", "testbinary"))
    {
      view.Execute();
      var rec = view.Fetch();

      var inStream = rec.GetStream("Data");
      if (inStream != null)
      {
        using (var file = File.OpenWrite("S:\\testfile.zip"))
        {
          CopyStream(inStream, file);
        }
      }
    }
  }

The code of CopyStream method can be taken from this answer of omnipresent Jon Skeet. Note that if you should do this from CA, you will reference the database object like session.Database, instead of creating it.