System.IO.Directory.GetFiles() 没有列出所有文件

System.IO.Directory.GetFiles() doesn't list all files

我正在使用 System.IO.Directory.GetFiles() 函数使用以下代码列出 C:\Windows\System32 中的所有可执行文件:

using System;
using System.Collections.Generic;
using System.IO;
namespace MyProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] files;
            files = System.IO.Directory.GetFiles(@"C:\Windows\System32", "*.exe");
        }
    }
}

它列出了 300 个可执行文件:

但是当我用 PowerShell 列出所有文件时,它有 446 个可执行文件:

例如:C:\Windows\System32\WinSAT.exe.
它没有列出此文件。
知道为什么它不显示可执行文件的完整列表吗?它遗漏了一些文件。

GetFiles 将在您遇到异常时停止。异常包括访问冲突。获取所有文件的唯一方法是使用递归逐个目录解析目录。请参阅下面将结果写入 xml 文件的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace WriteFileNamesXml
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        const string FOLDER = @"c:\temp";
        static XmlWriter writer = null;
        static void Main(string[] args)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            writer = XmlWriter.Create(FILENAME, settings);
            writer.WriteStartDocument(true);

            DirectoryInfo info = new DirectoryInfo(FOLDER);
            WriteTree(info);

            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();


        }
        static long WriteTree(DirectoryInfo info)
        {
            long size = 0;
            writer.WriteStartElement("Folder");
            try
            {
                writer.WriteAttributeString("name", info.Name);
                writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
                writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
                writer.WriteAttributeString("date", info.LastWriteTime.ToString());


                foreach (DirectoryInfo childInfo in info.GetDirectories())
                {
                    size += WriteTree(childInfo);
                }

            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
                Console.WriteLine(errorMsg);
                writer.WriteElementString("Error", errorMsg);
            }

            FileInfo[] fileInfo = null;
            try
            {
                fileInfo = info.GetFiles();
            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
                Console.WriteLine(errorMsg);
                writer.WriteElementString("Error", errorMsg);
            }

            if (fileInfo != null)
            {
                foreach (FileInfo finfo in fileInfo)
                {
                    try
                    {
                        writer.WriteStartElement("File");
                        writer.WriteAttributeString("name", finfo.Name);
                        writer.WriteAttributeString("size", finfo.Length.ToString());
                        writer.WriteAttributeString("date", info.LastWriteTime.ToString());
                        writer.WriteEndElement();
                        size += finfo.Length;
                    }
                    catch (Exception ex)
                    {
                        string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
                        Console.WriteLine(errorMsg);
                        writer.WriteElementString("Error", errorMsg);
                    }
                }
            }

            writer.WriteElementString("size", size.ToString());
            writer.WriteEndElement();
            return size;

        }
    }
}

我猜你是 运行 64 位操作系统,但尝试从编译为 32 位的 C# 应用程序访问此目录 运行。根据 64 位编程指南 Windows / 运行 32 位应用程序/文件系统重定向器 :

The %windir%\System32 directory is reserved for 64-bit applications on 64-bit Windows. Most DLL file names were not changed when 64-bit versions of the DLLs were created, so 32-bit versions of the DLLs are stored in a different directory. WOW64 hides this difference by using a file system redirector.

In most cases, whenever a 32-bit application attempts to access %windir%\System32, %windir%\lastgood\system32, or %windir%\regedit.exe, the access is redirected to an architecture-specific path.

Original Path                Redirected Path for 32-bit x86 Processes Redirected
%windir%\System32            %windir%\SysWOW64
%windir%\lastgood\system32   %windir%\lastgood\SysWOW64   
%windir%\regedit.exe         %windir%\SysWOW64\regedit.exe

因此您可以将应用程序编译为 x64 或通过 %windir%\SysWOW64 文件夹访问丢失的文件。