| 1 | namespace WixToolset.Samples |
| 2 | { |
| 3 | using System; |
| 4 | using System.Collections.Generic; |
| 5 | using System.IO; |
| 6 | using WixToolset.Dtf.WindowsInstaller; |
| 7 | |
| 8 | public class SampleCA |
| 9 | { |
| 10 | [CustomAction] |
| 11 | public static ActionResult SampleCA1(Session session) |
| 12 | { |
| 13 | using (Record msgRec = new Record(0)) |
| 14 | { |
| 15 | msgRec[0] = "Hello from SampleCA1!" + |
| 16 | "\r\nCLR version is v" + Environment.Version; |
| 17 | session.Message(InstallMessage.Info, msgRec); |
| 18 | session.Message(InstallMessage.User, msgRec); |
| 19 | } |
| 20 | |
| 21 | session.Log("Testing summary info..."); |
| 22 | SummaryInfo summInfo = session.Database.SummaryInfo; |
| 23 | session.Log("MSI PackageCode = {0}", summInfo.RevisionNumber); |
| 24 | session.Log("MSI ModifyDate = {0}", summInfo.LastSaveTime); |
| 25 | |
| 26 | string testProp = session["SampleCATest"]; |
| 27 | session.Log("Simple property test: [SampleCATest]={0}.", testProp); |
| 28 | |
| 29 | session.Log("Testing subdirectory extraction..."); |
| 30 | string testFilePath = "testsub\\SampleCAs.cs"; |
| 31 | if (!File.Exists(testFilePath)) |
| 32 | { |
| 33 | session.Log("Subdirectory extraction failed. File not found: " + testFilePath); |
| 34 | return ActionResult.Failure; |
| 35 | } |
| 36 | else |
| 37 | { |
| 38 | session.Log("Found file extracted in subdirectory."); |
| 39 | } |
| 40 | |
| 41 | session.Log("Testing record stream extraction..."); |
| 42 | string tempFile = null; |
| 43 | try |
| 44 | { |
| 45 | tempFile = Path.GetTempFileName(); |
| 46 | using (View binView = session.Database.OpenView( |
| 47 | "SELECT `Binary`.`Data` FROM `Binary`, `CustomAction` " + |
| 48 | "WHERE `CustomAction`.`Target` = 'SampleCA1' AND " + |
| 49 | "`CustomAction`.`Source` = `Binary`.`Name`")) |
| 50 | { |
| 51 | binView.Execute(); |
| 52 | using (Record binRec = binView.Fetch()) |
| 53 | { |
| 54 | binRec.GetStream(1, tempFile); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | session.Log("CA binary file size: {0}", new FileInfo(tempFile).Length); |
| 59 | string binFileVersion = Installer.GetFileVersion(tempFile); |
| 60 | session.Log("CA binary file version: {0}", binFileVersion); |
| 61 | } |
| 62 | finally |
| 63 | { |
| 64 | if (tempFile != null && File.Exists(tempFile)) |
| 65 | { |
| 66 | File.Delete(tempFile); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | session.Log("Testing record stream reading..."); |
| 71 | using (View binView2 = session.Database.OpenView("SELECT `Data` FROM `Binary` WHERE `Name` = 'TestData'")) |
| 72 | { |
| 73 | binView2.Execute(); |
| 74 | using (Record binRec2 = binView2.Fetch()) |
| 75 | { |
| 76 | Stream stream = binRec2.GetStream("Data"); |
| 77 | string testData = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd(); |
| 78 | session.Log("Test data: " + testData); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | session.Log("Listing components"); |
| 83 | using (View compView = session.Database.OpenView( |
| 84 | "SELECT `Component` FROM `Component`")) |
| 85 | { |
| 86 | compView.Execute(); |
| 87 | foreach (Record compRec in compView) |
| 88 | { |
| 89 | using (compRec) |
| 90 | { |
| 91 | session.Log("\t{0}", compRec["Component"]); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | session.Log("Testing the ability to access an external MSI database..."); |
| 97 | string tempDbFile = Path.GetTempFileName(); |
| 98 | using (Database tempDb = new Database(tempDbFile, DatabaseOpenMode.CreateDirect)) |
| 99 | { |
| 100 | // Just create an empty database. |
| 101 | } |
| 102 | using (Database tempDb2 = new Database(tempDbFile)) |
| 103 | { |
| 104 | // See if we can open and query the database. |
| 105 | IList<string> tables = tempDb2.ExecuteStringQuery("SELECT `Name` FROM `_Tables`"); |
| 106 | session.Log("Found " + tables.Count + " tables in the newly created database."); |
| 107 | } |
| 108 | File.Delete(tempDbFile); |
| 109 | |
| 110 | return ActionResult.Success; |
| 111 | } |
| 112 | |
| 113 | [CustomAction("SampleCA2")] |
| 114 | public static ActionResult SampleCustomAction2(Session session) |
| 115 | { |
| 116 | using (Record msgRec = new Record(0)) |
| 117 | { |
| 118 | msgRec[0] = "Hello from SampleCA2!"; |
| 119 | session.Message(InstallMessage.Info, msgRec); |
| 120 | session.Message(InstallMessage.User, msgRec); |
| 121 | } |
| 122 | return ActionResult.UserExit; |
| 123 | } |
| 124 | } |
| 125 | } |