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);
        }
        public static void CreateShortcutByCom(string shortcutPath, string targetPath, string iconPath, string description = null)
        {
            IShellLink link = new ShellLink() as IShellLink;

            if (link == null)
            {
                return;
            }

            link.SetDescription(description);
            link.SetPath(targetPath);
            link.SetIconLocation(iconPath, 5);
            // Convert from Keys string value to IShellLink 16-bit format
            // IShellLink: 0xMMVK
            //   MM = Modifier (Alt, Control, Shift)
            //   VK = Virtual key code
            //

            IPersistFile file = (IPersistFile)link;

            file.Save(shortcutPath, false);
            Marshal.ReleaseComObject(file);
            Marshal.ReleaseComObject(link);
        }