/// <summary> /// Prints the shortcut definition. /// </summary> /// <param name="shortcutDefinition">The shortcut definition.</param> private static void PrintShortcutDefinition(ShortcutDefinition shortcutDefinition) { Console.WriteLine("------------------------------------"); Console.WriteLine("FileLocation = " + shortcutDefinition.FileLocation); Console.WriteLine("TargetPath = " + shortcutDefinition.TargetPath); Console.WriteLine("Arguments = " + shortcutDefinition.Arguments); Console.WriteLine("Description = " + shortcutDefinition.Description); Console.WriteLine("HotKey = " + shortcutDefinition.HotKey); Console.WriteLine("IconLocation = " + shortcutDefinition.IconLocation); Console.WriteLine("RelativePath = " + shortcutDefinition.RelativePath); Console.WriteLine("WindowStyle = " + shortcutDefinition.WindowStyle); Console.WriteLine("WorkingDirectory = " + shortcutDefinition.WorkingDirectory); Console.WriteLine("------------------------------------"); }
/// <summary> /// Creates the shortcut. /// </summary> /// <param name="shortcutDefinition">The shortcut definition.</param> private static void CreateShortcut(ShortcutDefinition shortcutDefinition) { var shell = new WshShell(); var shortcut = (IWshShortcut)shell.CreateShortcut(shortcutDefinition.FileLocation); if (!string.IsNullOrEmpty(shortcutDefinition.Arguments)) { shortcut.Arguments = shortcutDefinition.Arguments; } if (!string.IsNullOrEmpty(shortcutDefinition.Description)) { shortcut.Description = shortcutDefinition.Description; } if (!string.IsNullOrEmpty(shortcutDefinition.HotKey)) { shortcut.Hotkey = shortcutDefinition.HotKey; } if (!string.IsNullOrEmpty(shortcutDefinition.IconLocation)) { shortcut.IconLocation = shortcutDefinition.IconLocation; } if (!string.IsNullOrEmpty(shortcutDefinition.RelativePath)) { shortcut.RelativePath = shortcutDefinition.RelativePath; } if (!string.IsNullOrEmpty(shortcutDefinition.TargetPath)) { shortcut.TargetPath = shortcutDefinition.TargetPath; } if (!string.IsNullOrEmpty(shortcutDefinition.WorkingDirectory)) { shortcut.WorkingDirectory = shortcutDefinition.WorkingDirectory; } shortcut.WindowStyle = shortcutDefinition.WindowStyle; shortcut.Save(); }
/// <summary> /// Defines the entry point of the application. /// </summary> /// <param name="args">The arguments.</param> static void Main(string[] args) { // Validate arguments if (args.Count() < 2) { PrintUsage(); Environment.Exit(1); } // Load shortcut from arguments var shortcutDefinition = new ShortcutDefinition { FileLocation = ConvertToLink(args[0]), TargetPath = args[1] }; for (int i = 2; i < args.Count(); i++) { if ((args[i].ToUpper() == "-A" || args[i].ToUpper() == "/A") && args.Count() > i + 1) { shortcutDefinition.Arguments = args[i + 1]; } if ((args[i].ToUpper() == "-D" || args[i].ToUpper() == "/D") && args.Count() > i + 1) { shortcutDefinition.Description = args[i + 1]; } if ((args[i].ToUpper() == "-H" || args[i].ToUpper() == "/H") && args.Count() > i + 1) { shortcutDefinition.HotKey = args[i + 1]; } if ((args[i].ToUpper() == "-I" || args[i].ToUpper() == "/I") && args.Count() > i + 1) { shortcutDefinition.IconLocation = args[i + 1]; } if ((args[i].ToUpper() == "-R" || args[i].ToUpper() == "/R") && args.Count() > i + 1) { shortcutDefinition.RelativePath = args[i + 1]; } if ((args[i].ToUpper() == "-S" || args[i].ToUpper() == "/S") && args.Count() > i + 1) { shortcutDefinition.WindowStyle = ConvertToWindowStyle(args[i + 1]); } if ((args[i].ToUpper() == "-W" || args[i].ToUpper() == "/W") && args.Count() > i + 1) { shortcutDefinition.WorkingDirectory = args[i + 1]; } } if (shortcutDefinition.TargetPath.ToLower() == "-remove" || shortcutDefinition.TargetPath.ToLower() == "-delete" || shortcutDefinition.TargetPath.ToLower() == "/remove" || shortcutDefinition.TargetPath.ToLower() == "/delete") { if (System.IO.File.Exists(shortcutDefinition.FileLocation)) { System.IO.File.Delete(shortcutDefinition.FileLocation); PrintSuccess("Successfully removed shortcut."); } Environment.Exit(0); } // Expand target path for (int i = 0; i < 5; i++) { shortcutDefinition.TargetPath = Environment.ExpandEnvironmentVariables(shortcutDefinition.TargetPath); } // Expand working directory if (!string.IsNullOrWhiteSpace(shortcutDefinition.WorkingDirectory)) { for (int i = 0; i < 5; i++) { shortcutDefinition.WorkingDirectory = Environment.ExpandEnvironmentVariables(shortcutDefinition.WorkingDirectory); } } // Print PrintShortcutDefinition(shortcutDefinition); // Validate arguments if (!System.IO.File.Exists(shortcutDefinition.TargetPath)) { PrintError("Target not found."); } if (!IsHotKey(shortcutDefinition.HotKey)) { PrintError("HotKey is invalid."); } if (!string.IsNullOrWhiteSpace(shortcutDefinition.RelativePath) && !Directory.Exists(shortcutDefinition.RelativePath)) { PrintError("Relative path not found."); } if (!string.IsNullOrWhiteSpace(shortcutDefinition.WorkingDirectory) && !Directory.Exists(shortcutDefinition.WorkingDirectory)) { PrintError("Working directory not found."); } try { CreateShortcut(shortcutDefinition); PrintSuccess("Successfully created shortcut."); } catch (Exception ex) { PrintError(ex.Message); } }