How to manage *.lnk files

Hello,

My winforms application is watching for each files inside one specific folder and treats file according to its type.

When I encounter a LNK file, a shortcut, I would like to access the file that is linked to that shortcut.

Is there a way to do this I have looked in system.io but found nothing. I am using .Net 1.1 still.

Thanks a lot for any help,

Claude



Answer this question

How to manage *.lnk files

  • nilsandrey

    Unfortunately, there's no managed wrapper for dealing with links. You could either use p/invoke to call into the shell API or, if you're not comfortable with p/invoke, there's a COM wrapper (WshShortcut) available in the Windows Script Host library.


  • DemianRulEZ

    The problem probably lies with how you're creating the shortcut object. It shouldn't be instantiated directly. Instead, you should call the CreateShortcut method of the WshShell object (http://msdn.microsoft.com/library/default.asp url=/library/en-us/script56/html/d91b9d23-a7e5-4ec2-8b55-ef6ffe9c777d.asp). e.g.:

    private void Form1_DragDrop(object sender, DragEventArgs e)
    {
    if (e.Data.GetData(DataFormats.FileDrop) != null)
    {
    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

    WshShell shell = new WshShellClass();
    WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(files[0]);

    MessageBox.Show(shortcut.TargetPath);
    }
    }


  • John Scragg

    Thanks you very much, it is perfect !!!!


  • Emilian

    Thanks !

    Anyone knows if there is plans for this in .Net 2.0 or even further in the future

    I made a small project using the interop and I get this error:

    "COM object with CLSID {A548B8E4-51D5-4661-8824-DAA1D893DFB2} is either not valid or not registered."

    Using a small webform that receives drag and drop :

    private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)

    {

    if (e.Data.GetData(DataFormats.FileDrop) != null)

    {

    string sResult = string.Empty;

    IWshRuntimeLibrary.WshShortcutClass oShortCut = null;

    try

    {

    // Error occurs on that line !

    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

    oShortCut = new IWshRuntimeLibrary.WshShortcutClass();

    oShortCut.Load( files[0] );

    sResult = oShortCut.IconLocation;

    }

    catch(Exception ex)

    {

    sResult = ex.Message;

    }

    MessageBox.Show(sResult);

    }

    }

    Is the p/invoke method straightforward

    Thanks for your help,

    Claude


  • How to manage *.lnk files