question

spinlock-8600 avatar image
0 Votes"
spinlock-8600 asked spinlock-8600 commented

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

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!!!

dotnet-csharp
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I did more testing. From CMD window, I did CD to that long path and it worked fine. DIR showed all the same files that GetFiles() returned to my program.

However, when I tried tests with TYPE and RENAME, they failed with "The system cannot find the path specified." error. Again hinting that the "path" is the problem, not the file.

CD had no problem with the path.

0 Votes 0 ·

Instead of removing the parenthesis you can try removing other characters, reducing the total length of the path.

Note that some of the .NET functions, classes and OS commands where not designed to work with long paths and filenames, longer than about 260 characters.

If your File.Exists(@"z:\backups...") incorrectly returns False, then try File.Exists(@"\\?\z:\backups...").

If type "z:\backups..." fails, then try type "\\?\z:\backups...".

0 Votes 0 ·

I only removed the parens for a test.

The path is valid. The Microsoft software is broken.

Plus, if you look closely, this filepath is part of an Adobe product installation.

The path is valid!

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered spinlock-8600 commented

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()



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your response, but white space has been valid in Windows' filepaths for a long time.

We have all seen "C:\Program Files (x86)" and this proves whitespace, and parenthesis, are valid in filepaths.

CD had no problem with it is additional proof it is a valid pathname.

The problem is that some Microsoft code can't handle it properly, not the pathname is invalid.

0 Votes 0 ·
spinlock-8600 avatar image
0 Votes"
spinlock-8600 answered spinlock-8600 commented

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It requires an escape character.
If you type \\(, you will see \(.

0 Votes 0 ·

Yes, I know. The strings in my code all have double "\", to document it here, I put the single slash only.

0 Votes 0 ·