Example #1
0
    /// <summary>
    ///     Create shortcut in current FilePath.
    /// </summary>
    /// <param name="linkFileName">shortcut name(include .lnk extension.)</param>
    /// <param name="targetPath">target FilePath</param>
    /// <param name="workingDirectory">working FilePath</param>
    /// <param name="arguments">arguments</param>
    /// <param name="hotkey">hot key(ex: Ctrl+Shift+Alt+A)</param>
    /// <param name="shortcutWindowStyle">window style</param>
    /// <param name="description">shortcut description</param>
    /// <param name="iconNumber">icon index(start of 0)</param>
    /// <returns>shortcut file FilePath.</returns>
    /// <exception cref="FileNotFoundException"></exception>
    public static string CreateShortcut(
        string linkFileName,
        string targetPath,
        string workingDirectory = "",
        string arguments        = "",
        string hotkey           = "",
        ShortcutWindowStyles shortcutWindowStyle = ShortcutWindowStyles.WshNormalFocus,
        string description = "",
        int iconNumber     = 0)
    {
        if (linkFileName.Contains(DefaultShortcutExtension) == false)
        {
            linkFileName = $"{linkFileName}{DefaultShortcutExtension}";
        }
        if (!File.Exists(targetPath))
        {
            throw new FileNotFoundException(targetPath);
        }
        if (workingDirectory == string.Empty)
        {
            workingDirectory = Path.GetDirectoryName(targetPath);
        }
        string iconLocation = $"{targetPath},{iconNumber}";

        if (Environment.Version.Major >= 4)
        {
            Type    shellType = Type.GetTypeFromProgID(WscriptShellName);
            dynamic shell     = Activator.CreateInstance(shellType);
            dynamic shortcut  = shell.CreateShortcut(linkFileName);

            shortcut.TargetPath       = targetPath;
            shortcut.WorkingDirectory = workingDirectory;
            shortcut.Arguments        = arguments;
            shortcut.Hotkey           = hotkey;
            shortcut.WindowStyle      = shortcutWindowStyle;
            shortcut.Description      = description;
            shortcut.IconLocation     = iconLocation;

            shortcut.Save();
        }
        else
        {
            Type   shellType    = Type.GetTypeFromProgID(WscriptShellName);
            object shell        = Activator.CreateInstance(shellType);
            object shortcut     = shellType.InvokeMethod("CreateShortcut", shell, linkFileName);
            Type   shortcutType = shortcut.GetType();

            shortcutType.InvokeSetMember("TargetPath", shortcut, targetPath);
            shortcutType.InvokeSetMember("WorkingDirectory", shortcut, workingDirectory);
            shortcutType.InvokeSetMember("Arguments", shortcut, arguments);
            shortcutType.InvokeSetMember("Hotkey", shortcut, hotkey);
            shortcutType.InvokeSetMember("WindowStyle", shortcut, shortcutWindowStyle);
            shortcutType.InvokeSetMember("Description", shortcut, description);
            shortcutType.InvokeSetMember("IconLocation", shortcut, iconLocation);

            shortcutType.InvokeMethod("Save", shortcut);
        }
        return(Path.Combine(Environment.CurrentDirectory, linkFileName));
    }
    /// <summary>
    /// Create shortcut in current path.
    /// </summary>
    /// <param name="linkFileName">shortcut name(include .lnk extension.)</param>
    /// <param name="targetPath">target path</param>
    /// <param name="workingDirectory">working path</param>
    /// <param name="arguments">arguments</param>
    /// <param name="hotkey">hot key(ex: Ctrl+Shift+Alt+A)</param>
    /// <param name="shortcutWindowStyle">window style</param>
    /// <param name="description">shortcut description</param>
    /// <param name="iconNumber">icon index(start of 0)</param>
    /// <returns>shortcut file path.</returns>
    /// <exception cref="System.IO.FileNotFoundException"></exception>
    public static string CreateShortcut(
        string linkFileName,
        string targetPath,
        string workingDirectory = "",
        string arguments        = "",
        string hotkey           = "",
        ShortcutWindowStyles shortcutWindowStyle = ShortcutWindowStyles.WshNormalFocus,
        string description = "",
        int iconNumber     = 0)
    {
        if (linkFileName.Contains(DEFAULT_SHORTCUT_EXTENSION) == false)
        {
            linkFileName = string.Format("{0}{1}", linkFileName, DEFAULT_SHORTCUT_EXTENSION);
        }
        if (File.Exists(targetPath) == false)
        {
            throw new FileNotFoundException(targetPath);
        }
        if (workingDirectory == string.Empty)
        {
            workingDirectory = Path.GetDirectoryName(targetPath);
        }
        string iconLocation = string.Format("{0},{1}", targetPath, iconNumber);

        if (Environment.Version.Major >= 4)
        {
            Type    shellType = Type.GetTypeFromProgID(WSCRIPT_SHELL_NAME);
            dynamic shell     = Activator.CreateInstance(shellType);
            dynamic shortcut  = shell.CreateShortcut(linkFileName);
            shortcut.TargetPath       = targetPath;
            shortcut.WorkingDirectory = workingDirectory;
            shortcut.Arguments        = arguments;
            shortcut.Hotkey           = hotkey;
            shortcut.WindowStyle      = shortcutWindowStyle;
            shortcut.Description      = description;
            shortcut.IconLocation     = iconLocation;
            shortcut.Save();
        }
        else
        {
            Type   shellType    = Type.GetTypeFromProgID(WSCRIPT_SHELL_NAME);
            object shell        = Activator.CreateInstance(shellType);
            object shortcut     = shellType.InvokeMethod("CreateShortcut", shell, linkFileName);
            Type   shortcutType = shortcut.GetType();
            shortcutType.InvokeSetMember("TargetPath", shortcut, targetPath);
            shortcutType.InvokeSetMember("WorkingDirectory", shortcut, workingDirectory);
            shortcutType.InvokeSetMember("Arguments", shortcut, arguments);
            shortcutType.InvokeSetMember("Hotkey", shortcut, hotkey);
            shortcutType.InvokeSetMember("WindowStyle", shortcut, shortcutWindowStyle);
            shortcutType.InvokeSetMember("Description", shortcut, description);
            shortcutType.InvokeSetMember("IconLocation", shortcut, iconLocation);
            shortcutType.InvokeMethod("Save", shortcut);
        }
        return(Path.Combine(System.Windows.Forms.Application.StartupPath, linkFileName));
    }