Win1020H2, .NET Framework 4.8, invalid results from System.IO.FileInfo() and File.Copy()

spinlock 1 Reputation point
2021-03-31T15:19:48.363+00:00

There is a bug in 20H2.NTFS or .NET Framework 4.8 System.IO.FileInfo() and File.Copy() (at least).

I wrote a C# program that walked the entire directory tree for a NTFS volume that had over 1.5M files. Once I had this list of files, I called System.IO.FileInfo on each one and found 166 files where FileInfo.Exists==false, which implies exactly what it says, that file does not exist. However, it does exist, I can see them with File Explorer, select Properties and get valid results, and I have verified their contents(a few are .MP4 and they play fine).

I figured there was some corruption in the NTFS tables, so I added recovery code to my program that was going to copy each file to \tmp, delete the original, then copy back (figuring this would create a fresh, valid entry).

However, the first File.Copy throws an exception, "Could not find a part of the path". All failures are crazy long paths, ex:
'Z:\backups\desktop\Cdrive\Program Files (x86)\Adobe\Adobe After Effects CS4\Support Files\Plug-ins\Effects\Synthetic Aperture(Color Finesse 2 Support)\Color Finesse 2\Color Finesse Presets\Gels\GamColor CineFilters\GamColor CineFilters_1-4 Plus Green.cfpreset'. I was suspicious of the 2nd set of parenthesis , "(Color Finesse 2 Support)". I renamed that directory, using File Explorer, removing the parenthesis , and now this file behaves normally!!!

Parenthesis are valid in directory and filenames. The files do exist and their contents are fine!!!

Obvious bug in NTFS or foundation classes!!!

Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-03-31T16:27:59.52+00:00

    Perhaps the white space in the path might be the issue. Try

    Public Module LanguageExtensions
        <Runtime.CompilerServices.Extension>
        Public Function AddQuotesIfRequired(path As String) As String
            Return If(Not String.IsNullOrWhiteSpace(path), If(path.Contains(" ") AndAlso (Not path.StartsWith("""") AndAlso Not path.EndsWith("""")), """" & path & """", path), String.Empty)
        End Function
    End Module
    

    Usage

    Dim somePath = "CS4\Support Files\Plug-ins\Effects\Synthetic Aperture(Color Finesse 2 Support)\Color Finesse 2\Color Finesse Presets\Gels\GamColor CineFilters\GamColor CineFilters_1-4 Plus Green.cfpreset"
    Dim result = somePath.AddQuotesIfRequired()
    

  2. spinlock 1 Reputation point
    2021-03-31T17:41:47.807+00:00

    OMG! Even this forum software, or the browser(I use Chrome), has a problem with this long string.

    In what I originally entered, there is a slash between "Synthetic Aperture" and "(Color Finesse 2 Support)", BUT it doesn't display!!!

    I copy/pasted that path in, but I thought it got lost somehow. NOPE! I checked twice with EDIT and the "\" is in the raw data, but the formatter, or the browser, doesn't display it!?!

    It's very interesting because those parens around "(Color Finesse 2 Support)" is definetely the problem area because I removed them with RENAME from File Explorer and everything was happy and worked.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.