Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to follow a .lnk file programmatically

We have a network drive full of shortcuts (.lnk files) that point to folders and I need to traverse them programmatically in a C# Winforms app.

What practical options do I have?

like image 653
user1231231412 Avatar asked Dec 28 '11 20:12

user1231231412


People also ask

How do you read a .LNK file?

How to open an LNK file. Opening an LNK file, by double-clicking it or right-clicking it and selecting Open, opens the file, folder, or program to which the LNK file points. Advanced users can edit an LNK file's properties by right-clicking the file and selecting Properties.

How do you find the location of a .LNK file?

LNK files are user profile specific in that LNK files are recorded per user on the system. Windows generated LNK files are stored in the folder C:\Users\ <username>\AppData\Roaming\Microsoft\Windows\Recent.

Are LNK files executable?

In Microsoft operating system, LNK files are the shortcut file to point to an executable. These shortcut files are used as a direct link to an executable file. Manipulated. LNK file's possible actions include masquerade system process to execute malware, Download malicious files from the internet, Delivery of .

What can you do with LNK files?

lnk are Windows shortcut files. These are just pointers in Windows that point to your original files. Dropbox can't follow these links, so you'll need to place the original files into your Dropbox folder in order to sync them.


2 Answers

Add IWshRuntimeLibrary as a reference to your project. Add Reference, COM tab, Windows Scripting Host Object Model.

Here is how I get the properties of a shortcut:

IWshRuntimeLibrary.IWshShell wsh = new IWshRuntimeLibrary.WshShellClass();
IWshRuntimeLibrary.IWshShortcut sc = (IWshRuntimeLibrary.IWshShortcut)wsh.CreateShortcut(filename);

The shortcut object "sc" has a TargetPath property.

like image 125
djdanlib Avatar answered Sep 18 '22 00:09

djdanlib


If you do not wish to reference COM, and distribute the Interop.IWshRuntimeLibrary.dll with your product (remembering Jay Riggs "Embed Interop Types": False)

You can use the new dynamic COM instead.

private void Window_Drop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
        dynamic shortcut;
        dynamic windowsShell;
        try
        {
            var file = files[0];
            if (Path.GetExtension(file)?.Equals(".lnk",StringComparison.OrdinalIgnoreCase) == true)
            {
                Type shellObjectType = Type.GetTypeFromProgID("WScript.Shell");
                windowsShell = Activator.CreateInstance(shellObjectType);
                shortcut = windowsShell.CreateShortcut(file);
                file = shortcut.TargetPath;
                // Release the COM objects
                shortcut = null;
                windowsShell = null;
            }
            //
            // <use file>...
            //
        }
        finally
        {
            // Release the COM objects
            shortcut = null;
            windowsShell = null;
        }
    }
}
like image 35
Michael Avatar answered Sep 19 '22 00:09

Michael