Example #1
0
        /// <summary>
        ///     Creates .lnk shortcut to filename
        /// </summary>
        /// <param name="filename">.lnk shortcut</param>
        /// <param name="path">path for filename</param>
        /// <param name="arguments">arguments for shortcut (can be null)</param>
        /// <returns>True if shortcut was created</returns>
        private static bool CreateShortcut(string filename, string path, string arguments)
        {
            var link = new PInvoke.ShellLink();

            ((PInvoke.IShellLinkW)link).SetPath(path);
            if (!string.IsNullOrEmpty(arguments))
            {
                ((PInvoke.IShellLinkW)link).SetArguments(arguments);
            }
            ((PInvoke.IPersistFile)link).Save(filename, false);

            return(File.Exists(filename));
        }
Example #2
0
        /// <summary>
        ///     Resolves path to .lnk shortcut
        /// </summary>
        /// <param name="shortcut">The path to the shortcut</param>
        /// <param name="filepath">Returns the file path</param>
        /// <param name="arguments">Returns the shortcuts arguments</param>
        /// <returns>Returns false if the filepath doesnt exist</returns>
        /// <remarks>
        ///     TODO: Read the .LNK file as documented at https://msdn.microsoft.com/en-us/library/dd871305.aspx
        /// </remarks>
        internal static bool ResolveShortcut(string shortcut, out string filepath, out string arguments)
        {
            var link = new PInvoke.ShellLink();

            ((PInvoke.IPersistFile)link).Load(shortcut, PInvoke.StgmRead);
            // TODO: if I can get hold of the hwnd call resolve first. This handles moved and renamed files.
            // ((IShellLinkW)link).Resolve(hwnd, 0)
            var path = new StringBuilder(PInvoke.MaxPath);

            PInvoke.Win32FindDataWide data;
            ((PInvoke.IShellLinkW)link).GetPath(path, path.Capacity, out data, 0);

            var args = new StringBuilder(PInvoke.MaxPath);

            ((PInvoke.IShellLinkW)link).GetArguments(args, args.Capacity);

            filepath  = path.ToString();
            arguments = args.ToString();

            return(FileExists(filepath));
        }