SafeDispatch/SetupCustomActions/InstallerClass.cs

217 lines
6.4 KiB
C#
Raw Normal View History

2024-02-22 16:43:59 +00:00
using Microsoft.Win32;
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Reflection;
namespace SetupAppServerLib
{
[RunInstaller(true)]
public partial class InstallerClass : System.Configuration.Install.Installer
{
public static String COMPANY = "Safemobile";
public static String APP = "Application Server";
public InstallerClass()
{
InitializeComponent();
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
// get the Application name from the CustomActionData parameters
APP = Context.Parameters["App"].ToString();
// insert the product code of the app into registry
CreateAppEntry(APP);
InsertRegValue("ProductCode", Context.Parameters["ProductCode"].ToString(), APP);
stateSaver.Add("ProductCode", Context.Parameters["ProductCode"].ToString());
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
protected override void OnAfterInstall(IDictionary stateSaver)
{
base.OnAfterInstall(stateSaver);
// THIS IS TRIGGERED BEFORE THIS INSTALLER CLASS IS CALLED, SO USELESS
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
protected override void OnBeforeInstall(IDictionary stateSaver)
{
base.OnBeforeInstall(stateSaver);
// THIS IS TRIGGERED BEFORE THIS INSTALLER CLASS IS CALLED, SO USELESS
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
protected override void OnCommitted(IDictionary stateSaver)
{
// same with Commit event
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
try
{
// get the path of the CustomAction dll file
String path = Context.Parameters["assemblypath"].ToString();
InsertRegValue("InstalledLocation", path.Substring(0, path.LastIndexOf('\\')), APP);
}
catch
{
// Do nothing...
}
//System.Diagnostics.Process.Start("http://www.microsoft.com");
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Rollback(IDictionary savedState)
{
base.Rollback(savedState);
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Uninstall(IDictionary savedState)
{
base.Uninstall(savedState);
APP = Context.Parameters["App"].ToString();
DeleteAppKey(APP);
}
#region Registry Entry Management
private bool CreateAppEntry(String appFolder)
{
// Write path to the registry
RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true);
if (key.OpenSubKey(COMPANY) == null)
key.CreateSubKey(COMPANY);
key = key.OpenSubKey(COMPANY, true);
try
{
key.CreateSubKey(appFolder);
return true;
}
catch (Exception)
{
return false;
}
}
private bool InsertRegValue(String regKey, String regValue, String appFolder)
{
// Write path to the registry
RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true);
if (key.OpenSubKey(COMPANY) == null)
key.CreateSubKey(COMPANY);
key = key.OpenSubKey(COMPANY, true);
key = key.OpenSubKey(appFolder, true);
try
{
key.SetValue(regKey, regValue);
return true;
}
catch (Exception)
{
return false;
}
}
private String GetRegValue(String regKey, String appFolder)
{
// Write path to the registry
RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\" + COMPANY
+ (appFolder.Equals(COMPANY) ? "" : "\\" + appFolder));
if (key != null)
{
try
{
return (String)key.GetValue(regKey);
}
catch (Exception)
{
return String.Empty;
}
}
return String.Empty;
}
private void DeleteRegValue(String regKey, String appFolder)
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\" + COMPANY
+ (appFolder.Equals(COMPANY) ? "" : "\\" + appFolder), true))
{
try
{
if (key == null)
{
// Key doesn't exist. Do whatever you want to handle
// this case
}
else
{
key.DeleteValue(appFolder);
}
}
catch (Exception ex)
{ }
}
}
private void DeleteAppKey(String appFolder)
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\" + COMPANY, true))
{
try
{
if (key == null)
{
// Key doesn't exist. Do whatever you want to handle
// this case
}
else
{
key.DeleteSubKeyTree(appFolder);
}
}
catch (Exception ex)
{ }
}
}
#endregion
}
}