using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using Squirrel; using Microsoft.Win32.TaskScheduler; using System.IO; namespace BodyshopPartner.Utils { public static class SquirrelAwareHelper { private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static string workingDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); public static bool AddHttpExcetion() { logger.Debug("Attempting to add HTTP Exclusion for web-server"); string args = string.Format(@"http add urlacl url={0} user={1}", "http://+:1337/", "Everyone"); ProcessStartInfo psi = new ProcessStartInfo("netsh", args); psi.Verb = "runas"; psi.CreateNoWindow = true; psi.WindowStyle = ProcessWindowStyle.Hidden; psi.UseShellExecute = true; try { Process.Start(psi).WaitForExit(); logger.Debug("Added web-server port exclusion."); return true; } catch (Exception Ex) { logger.Error("Unable to register exception " + Ex.ToString()); return false; } } public static bool CopyScripts() { logger.Debug("Attempting to copy scripts to global dir."); try { if (!Directory.Exists(AppMetaData.globalScriptsPath)) { Directory.CreateDirectory(AppMetaData.globalScriptsPath); } foreach (var srcPath in Directory.GetFiles(workingDirectory + @"\Utils\Scripts")) { File.Copy(srcPath, srcPath.Replace(workingDirectory + @"\Utils\Scripts", AppMetaData.globalScriptsPath), true); } return true; } catch (Exception Ex) { logger.Error("Unable to copy scripts " + Ex.ToString()); return false; } } public static void RemoveOldPaintScaleScript() { //The paint scale accidentally got added to all people running v31. This was added to remove it. try { Microsoft.Win32.TaskScheduler.Task existingTask = TaskService.Instance.FindTask("ImEX Online Partner - Paint Scale Connection"); if (existingTask != null) { TaskService.Instance.RootFolder.DeleteTask("ImEX Online Partner - Paint Scale Connection"); } } catch (Exception ex) { logger.Error(ex, "Error removing old paint scale task."); } } public static void InitializeSquirrelAware() { using (var mgr = new UpdateManager(Utils.UpdateHandler.UpdatePath)) { // Note, in most of these scenarios, the app exits after this method // completes! SquirrelAwareApp.HandleEvents( onInitialInstall: v => { logger.Debug("Installing via Squirrel Aware Helper. ", v); try { //Copy scripts from util directory to ImEX Utils. bool successful = CopyScripts(); if (successful) { logger.Debug("Successfully copied scripts"); } else { logger.Error("Unable to copy scripts."); } } catch (Exception Ex) { logger.Fatal(Ex, "Unable to copy scripts."); } try { //Copy scripts from util directory to ImEX Utils. bool successful = AddHttpExcetion(); if (successful) { logger.Debug("Successfully added exception."); } else { logger.Fatal("Unable to set exception for http port"); } } catch (Exception Ex) { logger.Fatal("Unable to set exception for http port", Ex.ToString()); } Utils.UpdateHandler.ToggleStartWithWindows(true); mgr.CreateShortcutForThisExe(); }, onAppUpdate: v => { mgr.CreateShortcutForThisExe(); Utils.UpdateHandler.RestoreSettings(); Utils.UpdateHandler.ToggleStartWithWindows(Properties.Settings.Default.StartWithWindows); CopyScripts(); RemoveOldPaintScaleScript(); }, onAppUninstall: v => { mgr.RemoveShortcutForThisExe(); try { Microsoft.Win32.TaskScheduler.Task existingTask = TaskService.Instance.FindTask("ImEX Online Partner - Paint Scale Connection Export"); //This is the old scale path. This was accidentally added for everyone. if (existingTask != null) { TaskService.Instance.RootFolder.DeleteTask("ImEX Online Partner - Paint Scale Connection"); } } catch (Exception ex) { logger.Error(ex, "Error removing task while uninstalling."); } try { Microsoft.Win32.TaskScheduler.Task existingTask = TaskService.Instance.FindTask("ImEX Online Partner - Paint Scale Connection Export"); if (existingTask != null) { TaskService.Instance.RootFolder.DeleteTask("ImEX Online Partner - Paint Scale Connection Export"); } } catch (Exception ex) { logger.Error(ex, "Error removing task while uninstalling."); } try { Microsoft.Win32.TaskScheduler.Task existingTask = TaskService.Instance.FindTask("ImEX Online Partner - ARMS"); if (existingTask != null) { TaskService.Instance.RootFolder.DeleteTask("ImEX Online Partner - ARMS"); } } catch (Exception ex) { logger.Error(ex, "Error removing task while uninstalling."); } }, onFirstRun: () => { logger.Info("We've run for the first time."); Utils.UpdateHandler.ToggleStartWithWindows(true); }); } } } }