Avatar of JamesNT
JamesNT

asked on 

Directory.GetFiles is skipping files

C# .Net Framework version 4.7.  

Directory.GetFiles is skipping files.  A lot of them.

File Explorer shows 635,286 while the count of Directory.GetFiles is 603,097.

Here is my code that is currently using EnumerateFiles but the results are the same as GetFiles.  

public void FindMyFolders()
        {
            conn = new SqlConnection("Persist Security Info=False;Integrated Security=true;Initial Catalog=NCTRIAL; Server=PROLAWPRIME");             conn.Open();             const string path = @"\\prolawprime\prolawfiles\Documents\";             IEnumerable<string> mainpath;             Console.WriteLine("Gathering Data...");             mainpath = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories);             Console.WriteLine("Number of files: " + mainpath.Count().ToString());             Console.ReadLine();

Open in new window

Any ideas on why GetFiles would skip files? 

I have tried unchecking Prefer 32-bit just in case.  Note that some files may have very long path names in excess of 383 characters.

Note that I receive no errors when this is done and am able to loop through the array (or IEnumerable) and place into a database.

Thank you for your help.

JamesNT

C#.NET Programming

Avatar of undefined
Last Comment
JamesNT
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

in the manifest did you allow for long filenames?

you may have to add manifest in visual studio
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:compatibility.v1">
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
    </windowsSettings>
  </application>
</assembly>

Open in new window

Avatar of JamesNT
JamesNT

ASKER

I'm actually running this in debug and getting the result.  I don't need to compile to an exe.  This is "one time code" just to get the list into a database.  Do I still need the manifest?

JamesNT
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

are the 32K files hidden?

Avatar of JamesNT
JamesNT

ASKER

I was under the impression GetFiles does not consider the hidden attribute when retrieving the file list.  Is that no true?

JamesNT
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

I don't remember for sure (and the help site is not explicit).

Can you test on a small subset?
Avatar of JamesNT
JamesNT

ASKER

Yes, GetFiles does get hidden files.  Tested on a folder with 3 files, one was hidden, and my string array.lenth = 3.

JamesNT
Avatar of JamesNT
JamesNT

ASKER

Ok, I think the problem is some of the file names are VERY long.  One is 383 characters.

If I use the approach of \\server\share\folder\etc then I get 603,114 files.
But if I use the other approach with the DFS namespace of \\corp.domainname.com\share\folder\etc then I get 593,254 files.  But I don't get an error on those files GetFiles() cannot see.  Is there a way to cause the error to verify?

JamesNT
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

have a look at the answer from https://stackoverflow.com/questions/5188527/how-to-deal-with-files-with-a-name-longer-than-259-characters

it says :
Use the \\?\C:\Verrrrrrrrrrrry long path syntax as described here.
ASKER CERTIFIED SOLUTION
Avatar of JamesNT
JamesNT

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo