1

I have a folder with symbolic links (cmd dir on that directory, lists these as SYMLINKD if that makes any difference). Any time I try to call Directory.GetFiles, Directory.GetDirectories() or any of the other variations in DirectoryInfo, I am given the following exception:

System.IO.DirectoryNotFoundException: Could not find a part of the path 'SYMBOLIC_LINK_PATH'.

I have also tried these with the directory prefixed by \\?\ (the "long path prefix" as mentioned here: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation)

How do I walk the directory and delete these symbolic links? What alternatives do I have to walk the directory?

(As suggested in a comment, I have also tried Directory.GetFiles("DirectorytoSearch", "*", SearchOption.AllDirectories) with the same result: DirectoryNotFoundException even though the MSDN article notes that this option should "include reparse points such as .... symbolic links")

derekantrican
  • 1,891
  • 3
  • 27
  • 57
  • 3
    According to the accepted answer for [this question](https://stackoverflow.com/questions/41179093/listing-all-files-in-a-directory-and-sub-directories-c-sharp), `Directory.GetFiles("DirectorytoSearch", "*", SearchOption.AllDirectories)` will include reparse points such as mounted drives and symbolic links in the search. Does that answer your question, or did I miss something and need to reopen? – Rufus L Jun 17 '20 at 19:51
  • Note my edit - using `Directory.GetFiles("DirectorytoSearch", "*", SearchOption.AllDirectories)` still throws an exception – derekantrican Jun 17 '20 at 21:05
  • Are you saying `Directory.EnumerateFiles(startDir, "*.lnk", SearchOption.AllDirectories).ToList().ForEach(x => File.Delete(x));` doesn't work for you? –  Jun 18 '20 at 12:59
  • No, that does not work. Still throws `DirectoryNotFoundException`. If I call `dir` on that directory, I see these are listed as `SYMLINKD` if that makes any difference – derekantrican Jun 22 '20 at 16:11
  • 1
    Do those symlinks now point to nonexistent directories? – Boluc Papuccuoglu Sep 30 '20 at 17:22
  • Indeed, this can happen if the link contains invalid directories. If you create a link with `mklink /D c:\temp\mylink c:\invalid1\invalid2\invalid3` for example, you will get "DirectoryNotFoundException: Could not find a part of the path 'c:\temp\mylink'." like the directory link itself doesn't exist, while `Directory.Exists(@"c:\temp\link")` returns true. So you can just catch this exception. – Simon Mourier Oct 02 '20 at 07:51
  • Yes, I think that's what's happening - when I'm deleting a bunch of files & subfolders in a directory I am deleting some of the symbolic link targets. So those links are now invalid links and throw an error when using `currentDirectory.GetFiles("*", SearchOption.AllDirectories);` – derekantrican Oct 06 '20 at 02:55

2 Answers2

1

The article you mentioned:
https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation

Was posted on 09/15/2020, which is pretty recent.
Surely you've updated the .NET runtime you're using, right?

Also, as a minimum Windows 10 version 1607 is required, double check if you're updated.

When and if you're updating your runtime, check if the latest update for it was at least released after the 09/15/2020.

If you're using the .NET Framework runtime, try doing this in the .NET Core runtime, since it's updated more frequently.

If all else fails, try this solution: https://stackoverflow.com/a/53439677/2536887,
else submit a bug report.

SubSpecs
  • 57
  • 10
0

Everyone's input helped me narrow down and figure out the problem. I believe the issue is that I had deleted the target of a symbolic link, but my recursive delete function still found it to be a directory. So when I tried to enumerate the files/directories of that symbolic link it threw a DirectoryNotFound exception (which is definitely confusing to encounter there because the directory still appears to exist - even in the file explorer, until you click on it).

The solution is that when trying to gather the children of a directory, catch DirectoryNotFound exceptions and try to delete the directory instead. That worked for me

        FileInfo[] files = null;
        try
        {
            files = currentDirectory.GetFiles("*", SearchOption.AllDirectories);
        }
        catch (DirectoryNotFoundException)
        {
            currentDirectory.Delete(true);
        }
derekantrican
  • 1,891
  • 3
  • 27
  • 57