3

I am just experimenting a little with my own folder browser. I notice that when I look at the System32 folder in Windows 7 I am getting some strange results. Here's the code:

DataTable dt=new DataTable();
string Folder="C:\\Windows\\System32";
DirectoryInfo DI = new DirectoryInfo(Folder);
foreach (FileInfo FI in DI.GetFiles())
{
    DataRow Row = dt.NewRow();
    if(FI.Name== "accelerometerdll.DLL")
    {

    }
    Row["Name"] = FI.Name;
    Row["Created"] = FI.CreationTime;

    Row["Size"] = FI.Length;
    dt.Rows.Add(Row);
}
dataGridView1.DataSource = dt;

When run, the list of files is incomplete. The total number of files is off by more than 400 files compared to windows explorer.

There is a simple check for the file named "accelerometerdll.dll" to try to resolve this issue. That file is, absolutely, located in the System32 folder. I can see it in Explorer and I can see it in the command window when I do a DIR. Yet it never shows up in my datatable. The condition is never met. Its like its simply invisible. I've tried running this as administrator with the same results.

Even more disconcerting is that it does show a file called "12520437.cpx" and I cannot see it in either explorer or the command window. It appears to be in the SysWOW64 folder not the System32 folder???

My main goal here is to show EXACTLY the same files that explorer shows when I open any folder.

Any ideas?

MethodMan
  • 18,625
  • 6
  • 34
  • 52
Jon Glazer
  • 783
  • 1
  • 10
  • 25
  • 2
    Is it your application a 32-bit application? Is it AnyCPU but with the "favor 32 bit" flag set? Are you using exactly the code you show (hard-coded path) or are you using Environment.GetSpecialFolder()? About the second question: GetFiles() returns all files, including hidden and protected ones (which might are invisible, by default, in Explorer). About your test: `==` is a case sensitive comparison. – Adriano Repetti May 19 '17 at 14:20
  • @AdrianoRepetti I am using Exactly the hard coded path as shown. Yes Any CPU and yes Prefer 32-bit. Didn't think these would have an impact (aside from the GetSpecialFolders()). – Jon Glazer May 19 '17 at 14:21
  • Then welcome to the world of the [File System Redirector](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx) – Damien_The_Unbeliever May 19 '17 at 14:22
  • @Damien_The_Unbeliever what does that mean? – Jon Glazer May 19 '17 at 14:22
  • 1
    That your 32 bit app will always see the redirected path. Too many app hard-coded the full path then you get ALWAYS redirected. – Adriano Repetti May 19 '17 at 14:23
  • 1
    If you follow the link, you'll see the considerable complexity that has gone into allowing 32-bit processes and 64-bit processes to access bit-appropriate content whilst both believing that they're accessing a folder called `C:\Windows\System32`. – Damien_The_Unbeliever May 19 '17 at 14:23
  • @AdrianoRepetti If you want to post an answer that I should use 64bit compiler directive, I'll accept it. I never thought about this and you solved it right away. Thanks! I am getting the correct numbers now. – Jon Glazer May 19 '17 at 14:25
  • 1
    Just remove "prefer 32 bit" and keep AnyCPU. – Adriano Repetti May 19 '17 at 14:26
  • @AdrianoRepetti Will do. You should post an answer so it gets indexed and may answer for others. – Jon Glazer May 19 '17 at 14:26
  • I'm away from my computer now, I won't ever try to write more than few words using cellphone! Feel free to self-answer including also damien's link to MSDN! – Adriano Repetti May 19 '17 at 14:29
  • Pretty much exact duplicate of [Directory.GetFiles Not returning a file](http://stackoverflow.com/questions/10600921/directory-getfiles-not-returning-a-file) – Raymond Chen May 19 '17 at 14:37
  • @RaymondChen I did search but didn't find that. Would have been nice to get the answer quicker. – Jon Glazer May 19 '17 at 16:32

1 Answers1

2

It was necessary to turn off the prefer 32 bit compiler directive based on knowledgeable responses.

More info is available here:

File System Redirector

Jon Glazer
  • 783
  • 1
  • 10
  • 25