Example #1
0
        public static IShellLink Create(string folderPath, string name, string target, string description = "", string arguments = "", string workingDirectory = "", string iconPath = "", int iconIndex = -1)
        {
            var shortcut = new ShellLink() as IShellLink;

            if (shortcut != null && !string.IsNullOrEmpty(target) && !string.IsNullOrEmpty(name))
            {
                // setup shortcut information
                shortcut.SetPath(target);
                if (!string.IsNullOrEmpty(description))
                {
                    shortcut.SetDescription(description);
                }
                if (!string.IsNullOrEmpty(iconPath) && iconIndex >= 0)
                {
                    shortcut.SetIconLocation(iconPath, iconIndex);
                }
                if (!string.IsNullOrEmpty(arguments))
                {
                    shortcut.SetArguments(arguments);
                }
                if (!string.IsNullOrEmpty(workingDirectory))
                {
                    shortcut.SetWorkingDirectory(workingDirectory);
                }

                // save it
                var file = (IPersistFile)shortcut;
                if (!name.EndsWith(".lnk"))
                {
                    name += ".lnk";
                }
                file.Save(Path.Combine(folderPath, name), false);
            }
            return(shortcut);
        }
Example #2
0
        public static Option <string> CreateShortcut(Some <string> shortcutPath, Some <string> path,
                                                     Option <string> arguments, Some <string> description, bool force)
        {
            if (File.Exists(shortcutPath.Value) && !force)
            {
                return(new Option <string>(shortcutPath));
            }
            if (File.Exists(shortcutPath.Value))
            {
                File.Delete(shortcutPath.Value);
            }

            // ReSharper disable once SuspiciousTypeConversion.Global
            var link = new ShellLink() as IShellLink;

            // setup shortcut information
            link?.SetDescription(description.Value);
            link?.SetPath(path.Value);
            arguments.IfSome(a => link?.SetArguments(a));
            // save it
            // ReSharper disable once SuspiciousTypeConversion.Global
            var file = link as IPersistFile;

            file?.Save(shortcutPath, false);
            return(!File.Exists(shortcutPath.Value) ? Option <string> .None : new Option <string>(shortcutPath));
        }