using IWshRuntimeLibrary;using System;using System.Collections;using System.Collections.Generic;using System.ComponentModel;using System.Configuration.Install;using System.IO;using System.Linq;using System.Security.AccessControl;using System.Security.Principal;using System.Threading.Tasks;
namespace ShortcutCreator{ /// <summary> /// Note: this installer MUST be added to all phases in de Custom Acounts of the setup. Leave 1 out and the dll will not be loaded causing a 1001 error. /// </summary> [RunInstaller(true)] public partial class ShortcutCreator : System.Configuration.Install.Installer { public ShortcutCreator() { InitializeComponent(); }
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)] public override void Install(IDictionary stateSaver) { base.Install(stateSaver); }
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)] public override void Uninstall(IDictionary stateSaver) { string location = Context.Parameters["location"]; if (string.IsNullOrEmpty(location)) return; if(!location.EndsWith(".lnk")) return;
System.IO.File.Delete(location);
base.Uninstall(stateSaver); }
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)] public override void Rollback(IDictionary stateSaver) { base.Rollback(stateSaver); }
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)] public override void Commit(IDictionary stateSaver) { base.Commit(stateSaver); CreateShortcut(); }
private void CreateShortcut() { string targetfile = Context.Parameters["targetfile"]; if (string.IsNullOrEmpty(targetfile)) return;
string location = Context.Parameters["location"]; if (string.IsNullOrEmpty(location)) return;
string iconPath = Context.Parameters["iconpath"]; if (string.IsNullOrEmpty(iconPath)) return; WshShell shell = new WshShell(); IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(location);
shortcut.Description = "NopFinance shortcut"; // The description of the shortcut shortcut.IconLocation = iconPath; // The icon of the shortcut shortcut.TargetPath = targetfile; // The path of the file that will launch when the shortcut is run shortcut.Save();
Log($"create shortcut at {location} to {targetfile}");
Log("Shortcut created added."); }
private void Log(string message) { Context.LogMessage(message); } }
}