// (c) 2005 kp@kp73.com
using System;
using System.Configuration.Install;
using System.IO;
using System.Collections;
using System.Diagnostics;
namespace KP.Tetris.Mce.CustomAction{
[System.ComponentModel.RunInstallerAttribute(true)]
public class MclUpdater : Installer
{
public override void Install(IDictionary savedState)
{
base.Install(savedState);
try
{
Debug.WriteLine("Install custom action " + this.ToString());
string applicationFolder = Path.GetDirectoryName(Context.Parameters["assemblypath"]);
Debug.WriteLine("Install application folder: " + applicationFolder);
Update(applicationFolder);
}
catch (Exception e)
{
Debug.WriteLine("Install error: " + e.Message);
throw;
}
}
public override void Uninstall(IDictionary savedState)
{
base.Uninstall (savedState);
try
{
string mcePrograms =
Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu),
@"Programs\Accessories\Media Center\Media Center Programs\DotNETris.mcl");
File.Delete(mcePrograms);
}
catch (Exception)
{
// don't stop uninstallation just 'cos we can't remove this file
// might end up with uninstall rolling back every time
}
}
public static void Update(string applicationFolder)
{
string mcl = null;
string mclFilePath = Path.Combine(applicationFolder, @"DotNETris.mcl");
Debug.WriteLine("Install reading file: " + mclFilePath);
using (FileStream s = File.OpenRead(mclFilePath))
{
StreamReader r = new StreamReader(s);
mcl = r.ReadToEnd();
}
mcl = mcl.Replace(@"{APPLICATIONFOLDER}", applicationFolder);
applicationFolder = applicationFolder.Replace(@"\", @"/");
mcl = mcl.Replace(@"{APPLICATIONFOLDER_URL}", applicationFolder);
Debug.WriteLine("Install updating file: " + mclFilePath);
FileStream outputStream = File.Create(mclFilePath);
StreamWriter outputWriter = new StreamWriter(outputStream);
outputWriter.Write(mcl);
outputWriter.Flush();
outputWriter.Close();
Debug.WriteLine("Install update complete file: " + mclFilePath);
try
{
string mcePath =
Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu),
@"Programs\Accessories\Media Center\Media Center Programs");
string mceProgram = Path.Combine(mcePath, @"DotNETris.mcl");
if (!Directory.Exists(mcePath))
Directory.CreateDirectory(mcePath);
File.Copy(mclFilePath, mceProgram);
}
catch (Exception exp)
{
// don't stop installation just 'cos we can't copy this file (non MCE machine?)
Debug.WriteLine(exp.Message);
}
}
}
}
|