Avatar of 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.


C#.NET Programming

Avatar of undefined
Last Comment

8/22/2022 - Mon
David Johnson, CD

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">
      <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>

Open in new window


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?

Éric Moreau

are the 32K files hidden?

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy

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

Éric Moreau

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

Can you test on a small subset?

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

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?

Éric Moreau

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.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question