Click here to Skip to main content
15,898,134 members
Articles / Programming Languages / C#

A Windows Installer Database Diff Tool

Rate me:
Please Sign up or sign in to vote.
4.68/5 (17 votes)
21 Dec 2004CPOL2 min read 70.4K   899   29  
A command line tool that generates XML and/or HTML Diff reports of two Windows installer databases.
using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Text;
using System.Diagnostics;

using Interop.msi;

namespace MSIDiff
{
	/// <summary>
	/// Summary description for Differ.
	/// </summary>
	public class Differ
	{
		private FileInfo m_file1;
		private FileInfo m_file2;

		private string m_diffXml;

		public Differ( FileInfo file1, FileInfo file2 )
		{
			Debug.Assert( file1 != null );
			Debug.Assert( file2 != null );

			m_file1 = file1;
			m_file2 = file2;

			Trace.WriteLine( string.Format( "Comparing files {0} and {1}", file1.FullName, file2.FullName ) );
		}

		public void MakeXml()
		{
			Trace.WriteLine( "Making Xml diff report" );

			Installer installer = new WindowsInstaller.Installer() as Installer;
			Debug.Assert( installer != null );

			Database database1 = installer.OpenDatabase( m_file1.FullName, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly );
			Database database2 = installer.OpenDatabase( m_file2.FullName, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly );

			MemoryStream stream = new MemoryStream();

			using ( DiffWriter writer = new DiffWriter( database1, m_file1.FullName, stream ) )
				writer.Diff( database2, m_file2.FullName );

			stream.Seek( 0, SeekOrigin.Begin );

			m_diffXml = new StreamReader( stream ).ReadToEnd();
			stream.Close();
		}

		public void SaveXml( string path )
		{
			Trace.WriteLine( "Saving Xml diff report to " + path );

			TextWriter writer = new StreamWriter( new FileStream( path, FileMode.Create, FileAccess.Write, FileShare.None ), Encoding.UTF8 );
			writer.Write( m_diffXml );
			writer.Close();
		}

		public void SaveHtml( string path )
		{
			Trace.WriteLine( "Making Html diff report" );

			XslTransform transform = new XslTransform();

			Stream stream = this.GetType().Assembly.GetManifestResourceStream( this.GetType(), "diff.xslt" );

			XmlReader reader = new XmlTextReader( stream );
			transform.Load( reader, null, AppDomain.CurrentDomain.Evidence );
			reader.Close();

			XmlDocument doc = new XmlDocument();
			doc.LoadXml( m_diffXml );

			Trace.WriteLine( "Saving Html diff report to " + path );
			XmlWriter writer = new XmlTextWriter( path, Encoding.UTF8 );
			transform.Transform( doc, null, writer, null );
			writer.Close();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10

It only went downhill from there.

Hey look, I've got a blog

Comments and Discussions