SetupExeBuilder.cs :  » Installers-Generators » WiX » Microsoft » Tools » WindowsInstallerXml » Extensions » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Installers Generators » WiX 
WiX » Microsoft » Tools » WindowsInstallerXml » Extensions » SetupExeBuilder.cs
//-------------------------------------------------------------------------------------------------
// <copyright file="SetupExeBuilder.cs" company="Microsoft">
//    Copyright (c) Microsoft Corporation.  All rights reserved.
//    
//    The use and distribution terms for this software are covered by the
//    Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
//    which can be found in the file CPL.TXT at the root of this distribution.
//    By using this software in any fashion, you are agreeing to be bound by
//    the terms of this license.
//    
//    You must not remove this notice, or any other, from this software.
// </copyright>
// 
// <summary>
// Setup.exe builder for Fabricator Extensions.
// </summary>
//-------------------------------------------------------------------------------------------------
namespace Microsoft.Tools.WindowsInstallerXml.Extensions{
    using System;
    using System.Diagnostics;
    using System.IO;

    /// <summary>
    /// Builder for setup.exe.
    /// </summary>
    public sealed class SetupExeBuilder
    {
        private FabricatorCore core;

        private string setupExePath;
        private string msiPath;

        /// <summary>
        /// Creates a new SetupExeBuilder object.
        /// </summary>
        /// <param name="core">Core build object for message handling.</param>
        public SetupExeBuilder(FabricatorCore core)
        {
            this.core = core;
            string assemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            this.setupExePath = Path.Combine(assemblyPath, "setup.exe");
        }

        /// <summary>
        /// Gets and sets the path to the MSI that is embedded in the setup.exe.
        /// </summary>
        /// <value>Path to MSI file.</value>
        public string MsiPath
        {
            get { return this.msiPath; }
            set { this.msiPath = value; }
        }

        /// <summary>
        /// Extracts the embedded package from this package.
        /// </summary>
        /// <param name="filePath">Path to this setup.exe to extract from.</param>
        /// <returns>Path to extracted embedded package.</returns>
        public static string GetEmbeddedPackage(string filePath)
        {
            string tempFileName = null;
            string extractPackagePath = null;

            try
            {
                // if the extension on the file path is ".exe" try to extract the MSI out of it
                tempFileName = Path.GetTempFileName();

                Process process = new Process();
                process.StartInfo.FileName = filePath;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.Arguments = String.Concat("-o1 \"", tempFileName, "\"");

                process.Start();
                process.WaitForExit();

                if (process.ExitCode != 0)
                {
                    throw new ApplicationException(String.Concat("Failed to extract MSI from ", process.StartInfo.FileName));
                }

                extractPackagePath = tempFileName; // the previous package path is now at the temp filename location
                tempFileName = null;
            }
            finally
            {
                if (tempFileName != null)
                {
                    File.Delete(tempFileName);
                }
            }

            return extractPackagePath;
        }

        /// <summary>
        /// Creates the setup.exe with embedded MSI.
        /// </summary>
        /// <param name="outputFile">Output path for setup.exe</param>
        /// <returns>True if build was successful, false if something went wrong.</returns>
        public bool Build(string outputFile)
        {
            if (null == this.msiPath)
            {
                throw new ArgumentNullException("MsiPath");
            }

            if (null == outputFile)
            {
                throw new ArgumentNullException("outputFile");
            }

            Directory.CreateDirectory(Path.GetDirectoryName(outputFile));

            int hr = NativeMethods.CreateSimpleSetup(this.setupExePath, this.msiPath, outputFile);
            if (hr != 0)
            {
                throw new ApplicationException(String.Format("Failed create setup.exe to: {0} from: {1}", outputFile, this.setupExePath));
            }

            return true;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.