4

I need to create a full list of the files and sub-directories in a directory.

DirectoryInfo.GetFiles() does NOT find all files; hidden files, at a minimum, seem to be missing.

(There may also be a problem with permissions, as I can't look inside some directories using Windows Explorer, even though I am running as Administrator. For instance, "C:\System Volume Information" can not be entered.)

I am using C#, Windows XP Pro

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
Mark T
  • 3,464
  • 5
  • 31
  • 45
  • 1
    The "C:\System Volume Inforamtion" folder cannot be viewed because it is assigned only to the SYSTEM user. In order to see inside of it you need to take ownership of it. However, this is not recommended for security reasons. – Alex Mendez Jul 25 '11 at 14:39
  • Possible duplicate of [C# - Get a list of files excluding those that are hidden](http://stackoverflow.com/questions/2418270/c-sharp-get-a-list-of-files-excluding-those-that-are-hidden) – TarmoPikaro May 27 '16 at 05:34

5 Answers5

7

DirectoryInfo.GetFiles() returns all files (excluding those that you don't have permission to see).

At the very least it definitely does include hidden files, as shown by this person who is asking almost exactly the reverse of this question.

Do you have a specific example of a file that appears elsewhere but not in this list?

Community
  • 1
  • 1
Justin
  • 84,773
  • 49
  • 224
  • 367
  • This is a partial answer, but see also Alex's comment, above, regarding permissions and file ownership. – Mark T Jul 28 '11 at 00:44
3

Weighing in at this late date, GetFiles DOES NOT always return all files, and I haven't figured out why either. Here is one way to reproduce (at least on 64-bit Windows 7 Home, running as Administrator and using C# with Visual Studio 2010).

Install the FTDI Drivers EXECUTABLE installer from here (http://www.ftdichip.com/Drivers/D2XX.htm)

This will install the following files in \Windows\System32:

-ftbusui.dll
-ftcserco.dll
-ftd2xx.dll
-FTLang.dll
-ftserui2.dll

The following code:

String[] files = Directory.GetFiles(Environment.SystemDirectory, "f*.*", SearchOption.TopDirectoryOnly);

returns ftd2xx.dll, but not the other four files.

Changing the searchPattern to *.*, or simply using:

GetFiles(Environment.SystemDirectory)

returns ftd2xx.dll, but not the other four files.

None of the files are hidden and all five have the same Owner and Permissions. All five files show up in Windows Explorer and in a Command Prompt window.

In fact, the following returns false:

File.Exists(@"c:\Windows\System32\ftbusui.dll")

and the four files don't show up in a OpenFileDialog dialog. Running the executable as Admin makes no difference and turning off UAC doesn't help.

Derek Johnson
  • 927
  • 1
  • 11
  • 15
  • I'm running into this as well. Have you found a solution? – Abtin Forouzandeh Jul 24 '13 at 02:04
  • This only happen with very specific files or with a wide range of files? It seems like this might be an issue with the FTDI drivers, trying to hide the driver files in a rootkit-like manner. – NirIzr Jul 19 '17 at 20:03
2

It should. Try to search in an other directory, create manually hidden file and see whether it will be in search resultset.

sll
  • 61,540
  • 22
  • 104
  • 156
2

DirectoryInfo.GetFiles() returns all files including hidden files.

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
1

As noted by the others, DirectoryInfo.GetFiles() gets all files. Thus, it looks like you might be hitting a permission issue.

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123