What does "GetFiles" do in system directories?
-
Hello, I have a funny question which arises from the effects I recognize in a sample app. At a certain point the sample app reads the files of a directory into an array. Since the path to "System32" was anyway pre-set in the sample, I realized the array gets filled with 2560 strings in the array - despite of more than 5000 files in that directory. After changing the path to another Windows directory ("System"), the array again filled with 2560 files only... No error in both cases. Then I changed the path to a different directory (holding approx. 6000 files), and the "GetFiles" array got filled with exactly the number of files existing. I tried this several times with other directories, and it seems that everything works (i.e. reads and counts) fine as long as the path isn't a system directory. Is this behaviour normal? Or what else might be going on? Thank you for any insights Mick
-
Hello, I have a funny question which arises from the effects I recognize in a sample app. At a certain point the sample app reads the files of a directory into an array. Since the path to "System32" was anyway pre-set in the sample, I realized the array gets filled with 2560 strings in the array - despite of more than 5000 files in that directory. After changing the path to another Windows directory ("System"), the array again filled with 2560 files only... No error in both cases. Then I changed the path to a different directory (holding approx. 6000 files), and the "GetFiles" array got filled with exactly the number of files existing. I tried this several times with other directories, and it seems that everything works (i.e. reads and counts) fine as long as the path isn't a system directory. Is this behaviour normal? Or what else might be going on? Thank you for any insights Mick
Sounds like you're running a 32-bit application on a 64-bit OS. The File System Redirector[^] intercepts your request to access
C:\Windows\System32
, and redirects it toC:\Windows\SysWOW64
instead. You can either compile your application as 64-bit; use the specialSysNative
alias for theSystem32
directory; or P/Invoke the Wow64DisableWow64FsRedirection[^] and Wow64RevertWow64FsRedirection[^] functions to disable the file system redirector for your application.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Sounds like you're running a 32-bit application on a 64-bit OS. The File System Redirector[^] intercepts your request to access
C:\Windows\System32
, and redirects it toC:\Windows\SysWOW64
instead. You can either compile your application as 64-bit; use the specialSysNative
alias for theSystem32
directory; or P/Invoke the Wow64DisableWow64FsRedirection[^] and Wow64RevertWow64FsRedirection[^] functions to disable the file system redirector for your application.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thank you, Richard, for sharing the knowledge as well as probable solutions. It's very likely that the 32/64bit thing is the reason for the effect. As I wrote before, it's a sample app I examined, so right now I don't need the solutions. But now I'm more prepared for that kind of possible surprises :rolleyes: There's still some queston marks, since SysWOW64 has only 2517 (not 2560) file entries on top-level, but it's good enough for having a first clue and some attention around that issue. Regards Mick
-
Hello, I have a funny question which arises from the effects I recognize in a sample app. At a certain point the sample app reads the files of a directory into an array. Since the path to "System32" was anyway pre-set in the sample, I realized the array gets filled with 2560 strings in the array - despite of more than 5000 files in that directory. After changing the path to another Windows directory ("System"), the array again filled with 2560 files only... No error in both cases. Then I changed the path to a different directory (holding approx. 6000 files), and the "GetFiles" array got filled with exactly the number of files existing. I tried this several times with other directories, and it seems that everything works (i.e. reads and counts) fine as long as the path isn't a system directory. Is this behaviour normal? Or what else might be going on? Thank you for any insights Mick
Be careful with the overloads:
string[] files = System.IO.Directory.GetFiles(@"C:\windows\system32", "*.*", SearchOption.AllDirectories);
looks into sub-directories, while the simple form
string[] files = System.IO.Directory.GetFiles(@"C:\windows\system32");
does not. I think that is the reason for the differences you experienced.
-
Be careful with the overloads:
string[] files = System.IO.Directory.GetFiles(@"C:\windows\system32", "*.*", SearchOption.AllDirectories);
looks into sub-directories, while the simple form
string[] files = System.IO.Directory.GetFiles(@"C:\windows\system32");
does not. I think that is the reason for the differences you experienced.
Thank you, Bernhard - that's indeed a very good point to look at. Unfortunately, right now I can't look up which overload was used in the sample... most probably I deleted it after having the code run for demonstration of its methods. But I'm going to check your hint later today.
-
Hello, I have a funny question which arises from the effects I recognize in a sample app. At a certain point the sample app reads the files of a directory into an array. Since the path to "System32" was anyway pre-set in the sample, I realized the array gets filled with 2560 strings in the array - despite of more than 5000 files in that directory. After changing the path to another Windows directory ("System"), the array again filled with 2560 files only... No error in both cases. Then I changed the path to a different directory (holding approx. 6000 files), and the "GetFiles" array got filled with exactly the number of files existing. I tried this several times with other directories, and it seems that everything works (i.e. reads and counts) fine as long as the path isn't a system directory. Is this behaviour normal? Or what else might be going on? Thank you for any insights Mick
Now, thanks to the insights of my helpers, I could make it clearer. I only checked the top-level number of files using
Directory.GetFiles(pfad, "*.*", SearchOption.TopDirectoryOnly))
Here's what I found: • there's no redirection to "SysWOW64" once "System32" is specified as path. I can get the files of both, once set as path. • the result 2560 was correct, but I could only verify this number after setting the system files (etc) to visible in the folder options! • i.e. the GetFiles method counts hidden files as well (which it is supposed to do) So in the end my question arose out of not considering that, in the Windows directory and it's subdirectories, many files are hidden and/or system files. The GetFiles method was completely ok, only my verification wasn't. Thank you Mick