-
-
Save isaveu/3fc71a39b6a16f9ef916dcb842295ac6 to your computer and use it in GitHub Desktop.
CreateDesktopShortcut
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public string GetInstalledAppFolderPath(Guid installGuid) | |
| { | |
| string result = string.Empty; | |
| try | |
| { | |
| using (RegistryKey parent = Registry.LocalMachine.OpenSubKey(SharedConstants.UninstallRegKeyPath, false)) | |
| { | |
| string guidText = installGuid.ToString("B"); | |
| RegistryKey key = parent.OpenSubKey(guidText, false); | |
| result = (string)key.GetValue("FolderPath", string.Empty); | |
| } | |
| } | |
| catch(Exception ex) | |
| { | |
| Trace.TraceError(ex.ToString()); | |
| } | |
| return result; | |
| } | |
| public bool TryCreateUninstaller(string appName, Guid installGuid, string folderPath, String exeName) | |
| { | |
| var exePath = Path.Combine(folderPath, exeName); | |
| try | |
| { | |
| using (RegistryKey parent = Registry.LocalMachine.OpenSubKey(SharedConstants.UninstallRegKeyPath, true)) | |
| { | |
| if (parent == null) | |
| { | |
| throw new Exception("Uninstall registry key not found."); | |
| } | |
| try | |
| { | |
| RegistryKey key = null; | |
| try | |
| { | |
| string guidText = installGuid.ToString("B"); | |
| key = parent.OpenSubKey(guidText, true) ?? | |
| parent.CreateSubKey(guidText); | |
| if (key == null) | |
| { | |
| throw new Exception(String.Format("Unable to create uninstaller '{0}\\{1}'", SharedConstants.UninstallRegKeyPath, guidText)); | |
| } | |
| key.SetValue("DisplayName", appName); | |
| key.SetValue("ApplicationVersion", "1"); | |
| key.SetValue("Publisher", "Autogramma"); | |
| key.SetValue("DisplayIcon", exePath); | |
| key.SetValue("DisplayVersion", "1"); | |
| key.SetValue("URLInfoAbout", "http://autogramma.ru"); | |
| key.SetValue("Contact", "http://autogramma.ru"); | |
| key.SetValue("InstallDate", DateTime.Now.ToString("yyyyMMdd")); | |
| key.SetValue("UninstallString", exePath + " d"); | |
| key.SetValue("FolderPath", folderPath); | |
| } | |
| catch (Exception ex) | |
| { | |
| Trace.TraceError(ex.ToString()); | |
| return false; | |
| } | |
| finally | |
| { | |
| if (key != null) | |
| { | |
| key.Close(); | |
| } | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| Trace.TraceError(ex.ToString()); | |
| return false; | |
| } | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| Trace.TraceError(ex.ToString()); | |
| return false; | |
| } | |
| return true; | |
| } | |
| public void CreateDesktopShortcut(string exePath, string appName) | |
| { | |
| string deskDir = CodeHelpers.GetAllUsersDesktopFolderPath(); | |
| using (StreamWriter writer = new StreamWriter(deskDir + "\\" + appName + "." + SharedConstants.ShortcutExtension)) | |
| { | |
| string app = exePath; | |
| writer.WriteLine("[InternetShortcut]"); | |
| writer.WriteLine("URL=file:///" + app); | |
| writer.WriteLine("IconIndex=0"); | |
| string icon = app.Replace('\\', '/'); | |
| writer.WriteLine("IconFile=" + icon); | |
| writer.Flush(); | |
| } | |
| } | |
| public void CreateDesktopShortcutLnk(string exePath, string appName) | |
| { | |
| string deskDir = CodeHelpers.GetAllUsersDesktopFolderPath(); | |
| WshShell shell = new WshShell(); | |
| string shortcutAddress = deskDir + "\\" + appName + "." + SharedConstants.ShortcutExtension; | |
| IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress); | |
| shortcut.Description = appName; | |
| shortcut.IconLocation = exePath; | |
| shortcut.WorkingDirectory = Path.GetDirectoryName(exePath); | |
| shortcut.TargetPath = exePath; | |
| shortcut.Save(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment