|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Windows.Forms; |
| 5 | +using Microsoft.Win32; |
| 6 | + |
| 7 | +namespace FlexInstaller |
| 8 | +{ |
| 9 | + public class SetupManager |
| 10 | + { |
| 11 | + private string temporaryFilePath; |
| 12 | + private string finalInstallLocation; |
| 13 | + |
| 14 | + public SetupManager() |
| 15 | + { |
| 16 | + temporaryFilePath = Path.Combine(Path.GetTempPath(), AppConfig.exeName); |
| 17 | + finalInstallLocation = Path.Combine(AppConfig.instPath, AppConfig.exeName); |
| 18 | + } |
| 19 | + |
| 20 | + public bool CreateDirectoryStructure() |
| 21 | + { |
| 22 | + try |
| 23 | + { |
| 24 | + if (!Directory.Exists(AppConfig.instPath)) |
| 25 | + { |
| 26 | + Directory.CreateDirectory(AppConfig.instPath); |
| 27 | + } |
| 28 | + return true; |
| 29 | + } |
| 30 | + catch (Exception ex) |
| 31 | + { |
| 32 | + MessageBox.Show(string.Format("Failed to create installation directory: {0}", ex.Message), "Installation Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 33 | + return false; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public bool InstallApplication(string downloadedFile) |
| 38 | + { |
| 39 | + try |
| 40 | + { |
| 41 | + if (!File.Exists(downloadedFile)) |
| 42 | + { |
| 43 | + MessageBox.Show("Downloaded file not found.", "Installation Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + FileInfo fileInfo = new FileInfo(downloadedFile); |
| 48 | + if (fileInfo.Length < 1024) |
| 49 | + { |
| 50 | + MessageBox.Show("Downloaded file appears to be invalid or corrupted.", "Installation Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 51 | + return false; |
| 52 | + } |
| 53 | + if (File.Exists(finalInstallLocation)) |
| 54 | + { |
| 55 | + try |
| 56 | + { |
| 57 | + File.Delete(finalInstallLocation); |
| 58 | + } |
| 59 | + catch |
| 60 | + { |
| 61 | + MessageBox.Show("Cannot overwrite existing installation. Please close " + AppConfig.appName + " and try again.", "File In Use", MessageBoxButtons.OK, MessageBoxIcon.Warning); |
| 62 | + return false; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + File.Move(downloadedFile, finalInstallLocation); |
| 67 | + |
| 68 | + CreateUninstaller(); |
| 69 | + |
| 70 | + if (AppConfig.createStartMenu) |
| 71 | + { |
| 72 | + CreateStartMenuLink(); |
| 73 | + } |
| 74 | + |
| 75 | + RegisterInControlPanel(); |
| 76 | + return true; |
| 77 | + } |
| 78 | + catch (Exception ex) |
| 79 | + { |
| 80 | + MessageBox.Show(string.Format("Installation failed: {0}", ex.Message), "Installation Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 81 | + return false; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public void CreateDesktopShortcutManual() |
| 86 | + { |
| 87 | + try |
| 88 | + { |
| 89 | + string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); |
| 90 | + string shortcutPath = Path.Combine(desktopPath, AppConfig.appName + ".lnk"); |
| 91 | + |
| 92 | + if (File.Exists(shortcutPath)) |
| 93 | + { |
| 94 | + File.Delete(shortcutPath); |
| 95 | + } |
| 96 | + |
| 97 | + ProcessStartInfo psi = new ProcessStartInfo(); |
| 98 | + psi.FileName = "powershell.exe"; |
| 99 | + psi.Arguments = string.Format("-Command \"$WshShell = New-Object -comObject WScript.Shell; $Shortcut = $WshShell.CreateShortcut('{0}'); $Shortcut.TargetPath = '{1}'; $Shortcut.WorkingDirectory = '{2}'; $Shortcut.Save()\"", |
| 100 | + shortcutPath, finalInstallLocation, AppConfig.instPath); |
| 101 | + psi.WindowStyle = ProcessWindowStyle.Hidden; |
| 102 | + psi.CreateNoWindow = true; |
| 103 | + Process.Start(psi).WaitForExit(); |
| 104 | + } |
| 105 | + catch |
| 106 | + { |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private void CreateDesktopLink() |
| 111 | + { |
| 112 | + } |
| 113 | + |
| 114 | + private void CreateStartMenuLink() |
| 115 | + { |
| 116 | + } |
| 117 | + |
| 118 | + private void CreateUninstaller() |
| 119 | + { |
| 120 | + try |
| 121 | + { |
| 122 | + string uninstallerPath = Path.Combine(AppConfig.instPath, "uninstall.exe"); |
| 123 | + |
| 124 | + File.Copy(Application.ExecutablePath, uninstallerPath, true); |
| 125 | + } |
| 126 | + catch |
| 127 | + { |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private void RegisterInControlPanel() |
| 132 | + { |
| 133 | + try |
| 134 | + { |
| 135 | + string uninstallKey = string.Format(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{0}", AppConfig.appName); |
| 136 | + string uninstallerPath = Path.Combine(AppConfig.instPath, "uninstall.exe"); |
| 137 | + |
| 138 | + using (RegistryKey key = Registry.LocalMachine.CreateSubKey(uninstallKey)) |
| 139 | + { |
| 140 | + key.SetValue("DisplayName", AppConfig.appName); |
| 141 | + key.SetValue("DisplayVersion", AppConfig.appVer); |
| 142 | + key.SetValue("Publisher", AppConfig.pubName); |
| 143 | + key.SetValue("InstallLocation", AppConfig.instPath); |
| 144 | + key.SetValue("UninstallString", string.Format("\"{0}\"", uninstallerPath)); |
| 145 | + key.SetValue("DisplayIcon", finalInstallLocation); |
| 146 | + key.SetValue("HelpLink", AppConfig.supportUrl); |
| 147 | + key.SetValue("URLInfoAbout", AppConfig.website); |
| 148 | + key.SetValue("NoModify", 1); |
| 149 | + key.SetValue("NoRepair", 1); |
| 150 | + } |
| 151 | + } |
| 152 | + catch |
| 153 | + { |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public void LaunchInstalledApplication() |
| 158 | + { |
| 159 | + try |
| 160 | + { |
| 161 | + if (File.Exists(finalInstallLocation)) |
| 162 | + { |
| 163 | + ProcessStartInfo startInfo = new ProcessStartInfo(); |
| 164 | + startInfo.FileName = finalInstallLocation; |
| 165 | + startInfo.WorkingDirectory = AppConfig.instPath; |
| 166 | + startInfo.UseShellExecute = true; |
| 167 | + Process.Start(startInfo); |
| 168 | + } |
| 169 | + } |
| 170 | + catch (Exception ex) |
| 171 | + { |
| 172 | + MessageBox.Show(string.Format("Could not launch application: {0}", ex.Message), "Launch Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + public string GetTemporaryPath() |
| 177 | + { |
| 178 | + return temporaryFilePath; |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments