1

A project searches several Shortcuts to find their path's. It also creates Shortcuts.

After reading many posts, it seems there multiple ways to approach this, some using Reference to: COM -> Windows Script Host Object Model, and some without. Is it a performance burden to use the options that require Adding this Reference?

I located a post that showed how to use old VB code to create a Shortcut and posted below in case it helps anyone and to ask if this way is somehow less performance stress than using a Reference to: Windows Script Host Object Model

string[] myLines = {"set WshShell = WScript.CreateObject(\"WScript.Shell\")",
    "strDesktop = \"" + destinationDir + "\"",
    "set oShellLink = WshShell.CreateShortcut(\"" + path2shortcut + "\")"
    "oShellLink.TargetPath = \"" + targetPath + "\"",
    "oShellLink.WindowStyle = 1",
    "oShellLink.HotKey = \"CTRL+SHIFT+F\"",
    "oShellLink.IconLocation = \"notepad.exe, 0\"",
    "oShellLink.WorkingDirectory = strDesktop",
    "oShellLink.Save()" };
System.IO.File.WriteAllLines("test.vbs", myLines);
System.Diagnostics.Process P = System.Diagnostics.Process.Start("test.vbs");
P.WaitForExit(int.MaxValue);
System.IO.File.Delete("test.vbs");

Because above does not require to Add Reference, Windows Script Host Object Model, I am wondering if its better for performance to use a way to get a Shortcut's path, that also does not require a Reference to Windows Script Host Object Model.

Here are 2 options of ways found to search Shortcuts.

option 1) uses Reference to COM -> Windows Script Host Object Model..

WshShell shell = new WshShell();
link = (IWshShortcut)shell.CreateShortcut(linkPathName);
MessageBox.Show(link.TargetPath);

option 2) Does not use Reference, it uses FileStream, a user named Blez shows how to do without Adding a Reference [Here is the link- https://blez.wordpress.com/2013/02/18/get-file-shortcuts-target-with-c/]

FileStream fileStream = File.Open(file, FileMode.Open, FileAccess.Read)
using (System.IO.BinaryReader fileReader = new BinaryReader(fileStream))
{
    fileStream.Seek(0x14, SeekOrigin.Begin);     // Seek to flags
    uint flags = fileReader.ReadUInt32();        // Read flags
    // ... code continues for another 15 lines
}

Are either of these options better for Performance if iterating many shortcuts (approximately 100) at a time? I am unsure of the burden this puts on performance so I thought maybe it would be wise to use a 'using' Statement? (perhaps it is not possible or 'overkill') I have tried many ways and not found a way to do this. I even tried to Reference the DLL directly with: '[System.Runtime.InteropServices.DllImport("shell32.dll")]' and still no luck.

So I ask your help to find the best performance for Creating Shortcuts & Searching for Path's of Shortcuts.

Any input is welcome. I tried to make a simple and specific as possible. I appreciate your help SO!

1 Answer 1

1

Following is partial answer to your question:

Yours are the only ways to create shortcut files that I know of. This is fastest way that I've encountered for reading target path of shortcut file. This also uses Shell32 reference.

public static string GetShortcutTargetFile(string shortcutFilename)
    {
        string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
        string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);

        Shell shell = new Shell();
        Folder folder = shell.NameSpace(pathOnly);
        FolderItem folderItem = folder.ParseName(filenameOnly);
        if (folderItem != null)
        {
            Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
            return link.Path;
        }

        return string.Empty;
    }

I've tested this for reading target path of 100 shortcut files. For my machine it reads those paths in 0.9 seconds.

1
  • Great! Thanks for the information, are you aware of any performance issues from using the Reference to Shell32 ? (which i think is only available by Add reference to the Windows Script Host Object Model)
    – Budapest
    Commented Jul 2, 2016 at 1:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.