/// <summary>Creates a new lnk/url shortcut (Shell32). Further editing with 'LinkSetProperty'.</summary> /// <param name="shortcut">The full path for the new lnk/url shortcut file.</param> /// <param name="target">The full path of the target file/-folder resp. URL address.</param> /// <returns>The full file path of the created shortcut on success, else "FAILED".</returns> public static Primitive LinkCreate(Primitive shortcut, Primitive target) { if (String.IsNullOrEmpty(target)) { return("FAILED"); } string scPath = Environment.ExpandEnvironmentVariables(shortcut); string scDir = Path.GetDirectoryName(scPath); if (!Directory.Exists(scDir)) { return("FAILED"); } string scFNameExt = Path.GetFileName(scPath); try { System.IO.StreamWriter sw = new System.IO.StreamWriter(scPath, false); sw.Close(); Shell32.Folder fold = GetShell32NameSpace(scDir); Shell32.FolderItem foldItem = fold.ParseName(scFNameExt); Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)foldItem.GetLink; link.Path = target; link.Save(scPath); return(scPath); } catch { return("FAILED"); } }
/// <summary>Modifies properties of an existing lnk/url shortcut link, like target path, arguments etc. (* for Urls).</summary> /// <param name="shortcut">The full path of an existing lnk/url shortcut link file.</param> /// <param name="target">* The full path of the target file/-folder resp. URL address.</param> /// <param name="args">Startparameter when launching the shortcut or "" (for url).</param> /// <param name="folder">The full path of the start folder or "" (for url).</param> /// <param name="desc">* Comment or description for the shortcut or "".</param> /// <param name="icoPath">* Full path of the icon file for the shortcut or "".</param> /// <param name="icoIdx">* Index of the icon in the icon file (default: 0, for .ico).</param> /// <param name="hotkey">* keys combination to launch the shortcut link (default: 0).</param> /// <param name="style">* Window style when launching the shortcut (default: 1 normal, 3 max, 7 min).</param> /// <returns>The full file path of the modified shortcut on success, else "FAILED".</returns> public static Primitive LinkSetProperty(Primitive shortcut, Primitive target, Primitive args, Primitive folder, Primitive desc, Primitive icoPath, Primitive icoIdx, Primitive hotkey, Primitive style) { string scPath = Environment.ExpandEnvironmentVariables(shortcut); if (!System.IO.File.Exists(scPath)) { return("FAILED"); } else { string scDir = Path.GetDirectoryName(scPath); string scFNameExt = Path.GetFileName(scPath); Shell32.Folder fold = GetShell32NameSpace(scDir); Shell32.FolderItem foldItem = fold.ParseName(scFNameExt); try { Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)foldItem.GetLink; link.Path = target; link.Arguments = args; link.WorkingDirectory = folder; link.Description = desc; link.Hotkey = hotkey; link.ShowCommand = style; link.SetIconLocation(icoPath, icoIdx); link.Save(scPath); return(scPath); } catch { return("FAILED"); } } }