| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.IO; |
| 4 | using System.Linq; |
| 5 | using System.Reflection; |
| 6 | using System.Text; |
| 7 | using System.Threading.Tasks; |
| 8 | |
| 9 | namespace Core.Librarys |
| 10 | { |
| 11 | public class Shortcut |
| 12 | { |
| 13 | ///<summary> |
| 14 | /// |
| 15 | ///</summary> |
| 16 | ///<param name="lnkFilePath"></param> |
| 17 | ///<param name="args"></param> |
| 18 | private static bool CreateShortcut(string lnkFilePath, string args) |
| 19 | { |
| 20 | try |
| 21 | { |
| 22 | var shellType = Type.GetTypeFromProgID("WScript.Shell"); |
| 23 | dynamic shell = Activator.CreateInstance(shellType); |
| 24 | var shortcut = shell.CreateShortcut(lnkFilePath); |
| 25 | shortcut.TargetPath = Assembly.GetEntryAssembly().Location; |
| 26 | shortcut.Arguments = args; |
| 27 | shortcut.WorkingDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; |
| 28 | shortcut.Save(); |
| 29 | return true; |
| 30 | } |
| 31 | catch (Exception e) |
| 32 | { |
| 33 | Logger.Error(e.ToString()); |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | ///<summary> |
| 38 | /// |
| 39 | ///</summary> |
| 40 | ///<param name="startup"></param> |
| 41 | ///<returns></returns> |
| 42 | public static bool SetStartup(bool startup = true) |
| 43 | { |
| 44 | string path = Path.Combine( |
| 45 | Environment.GetFolderPath(Environment.SpecialFolder.Startup), |
| 46 | "Tai.lnk"); |
| 47 | if (startup) |
| 48 | { |
| 49 | if (File.Exists(path)) |
| 50 | { |
| 51 | return true; |
| 52 | } |
| 53 | return CreateShortcut(path, ""); |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | if (File.Exists(path)) |
| 58 | { |
| 59 | File.Delete(path); |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | } |
| 64 | ///<summary> |
| 65 | /// |
| 66 | ///</summary> |
| 67 | ///<param name="create"></param> |
| 68 | ///<returns></returns> |
| 69 | public static bool CreateDesktopShortcut() |
| 70 | { |
| 71 | return CreateShortcut(Path.Combine( |
| 72 | Environment.GetFolderPath(Environment.SpecialFolder.Desktop), |
| 73 | "Tai.lnk"), "--selfStart"); |
| 74 | } |
| 75 | } |
| 76 | } |