C# trying to getshortcuttargetfile to parse lnk file

375 Views Asked by At

I'm trying to build a simple archiving program. We have files scattered throughout the system so part of the routine checks to see if a shortcut was put into the system and if so, to fetch all of the files from where the shortcut points to. It was all working fine a few weeks ago, but when I applied some visual studio updates it seems to have ceased working.

    var downloadFilePath = Path.Combine(storFolder, caseEntity.ID, caseDocs.FileName);

    if (downloadFilePath.Contains(".lnk"))
    {
        try
        {
            Console.WriteLine("Link Found. Now Processing.");

            DirectoryInfo source = new DirectoryInfo(GetShortcutTargetFile(downloadFilePath));

            DirectoryInfo target = new DirectoryInfo(storFolder + "\\" + nuFolder);
            CopyFilesRecursively(source, target);

        }
        catch { Console.WriteLine("Bad link, no documents found."); }
    }

Using break points it looks like the problem is in the "DirectoryInfo source..." line but I can't figure out why it's failing.

This is the GetShortcutTargetFile function.

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

            Shell shell = new Shell();
            Folder folder = shell.NameSpace(pathOnly);
            FolderItem folderItem = folder.ParseName(filenameOnly);
            if (folderItem != null)
            {
                ShellLinkObject link = (ShellLinkObject)folderItem.GetLink;
                return link.Path;
            }
            return string.Empty; // not found
        }
0

There are 0 best solutions below