1

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
        }
Nathan Mc
  • 21
  • 7
  • What dose it says – Alen.Toma Aug 12 '21 at 19:09
  • I can't tell what it says. When I ask it to step past the breakpoint it triggers the try/catch and fails out immediately to the "bad link" error message. – Nathan Mc Aug 12 '21 at 19:18
  • 1
    Update your catch to `catch (Exception ex) { Console.WriteLine($"{ex} Bad link, no documents found."); }` to get the error. – Moir Aug 12 '21 at 19:27
  • Thanks @Moir. Did so and it gives the error: System.IO.DirectoryNotFoundException: Could not find a part of the path 'V:\2018-11\99999 Example - Files'. Which is odd because I checked and that path is valid. – Nathan Mc Aug 12 '21 at 19:56
  • DirectoryInfo is completely reliable - and it doesn't verify the existence of the directory passed into the constructor so "source" probably isn't the source of your error. What does GetShortcutTargetFile do (apart from return the target file)? – stemixrf Aug 12 '21 at 23:13
  • @NathanMc is `V:\2018-11\99999 Example - Files` the source? Are you escaping the \ ? – Moir Aug 13 '21 at 01:12
  • @Moir - the source is the .lnk file that the system downloads. It's pulled from the "var downloadFilePath...etc" line. – Nathan Mc Aug 13 '21 at 12:35
  • @stemixrf i will update the post with the function. – Nathan Mc Aug 13 '21 at 12:36
  • If "V:\" is mapped network drive, could be related to access rights. Also the path returned from 'GetShortcutTargetFile' could be a file path. But then the exception would be thrown in CopyFileRecursivly... – lidqy Aug 14 '21 at 01:47
  • @NathanMc, do you have an inner exception? Could be the shell methods don't play well with spaces in paths, maybe you need quotes around it. Wouldn't surprise me but it's a couple of years since I did any shell stuff so don't recall and things change anyway. Worth checking if you have an inner exception or you can try/catch in GetShortcutTargetFile pick up the error there. I'm reasonably sure from what you say that the error is in that method not DirectoryInfo source.... – stemixrf Aug 14 '21 at 02:57
  • @stemixrf - thanks I'll give that a try and see how it does. – Nathan Mc Aug 16 '21 at 13:19
  • I set up exceptions check in the GetShortcutTargetFile and some breakpoints to step through. The sub program worked perfectly with no errors and returned link.Path successfully. As a test, I created a shortcut without any spaces and tried to run it through the program - it also failed with "Could not find part of the path...etc" message. – Nathan Mc Aug 16 '21 at 15:16

0 Answers0