为什么FindFirstFile/FindNextFile没有列出Windows 7上系统目录的全部内容?

Jho*_*123 3 delphi winapi windows-7 delphi-xe3

我有一个小问题.API FindNextFile不会列出目录的全部内容C:\Windows\System32(仅限Windows 7).有人有任何解决方案?

代码[Delphi]:

Var
  sAtr:       String;
  sPathName:  String;
  I:          Integer;
  iCont:      Integer;
  tHnd:       THandle;
  tArrAtr:    TStringList;
  tWDF:       WIN32_FIND_DATA;
Begin
  iCont := 0;
  sAtr := '';
  Result := TStringList.Create;
  tArrAtr := TStringList.Create;
  tHnd := FindFirstFile(PChar(sPath + '*.*'), tWDF);

  If RightStr(sPath, 1) <> '\' Then
    sPath := sPath + '\';

  If tHnd = INVALID_HANDLE_VALUE Then
    Exit;

  Repeat
    If (tWDF.dwFileAttributes And FILE_ATTRIBUTE_ARCHIVE) > 0 Then
      If (String(tWDF.cFileName[0]) <> '.') Then
      Begin
        sPathName := sPath + String(tWDF.cFileName);

        Result.Add(String(tWDF.cFileName) + sDel +
                   GetFileSizeAPI(sPathName));
        sAtr := '';
        Inc(iCont);
      End;
  Until (FindNextFile(tHnd, tWDF) <> True);

  //CloseHandle(tHnd);
Run Code Online (Sandbox Code Playgroud)

Dav*_*nan 7

我打赌你有64位机器和32位进程.该文件系统重定向器也发挥了作用,并System32重定向到SysWOW64.

避免重定向器的最佳方法是执行64位进程.或者您可以列出Sysnative从32位进程获取64位系统文件夹.你甚至可以禁用文件系统重定向器,但这是一件非常危险的事情,我不推荐它.

此外,您通过调用FindClose而不是调整查找句柄CloseHandle.您应该在调用之前添加反斜杠FindFirstFile.并测试特殊...通过比较全名与这些特殊值.