Detecting if a directory is a junction in Delphi

点点圈 提交于 2019-12-21 03:26:15

问题


I've been Google searching this I may be having some brain clouds because it just isn't working.

I need to detect if a folder is a junction so my recursive file search doesn't run off into an endless loop.

I could use a simple function like

IsJunction(attr: dword): boolean; 

where attr is dwFileAttributes from TWin32FindData;

I just can't seem to get it to work. Thanks!


回答1:


dwFileAttributes of TWin32FindData does not have that information, you have to look to the dwReserved0 field. See documentation.

function IsJunction(const FileName: string): Boolean;
//  IO_REPARSE_TAG_MOUNT_POINT = $A0000003;
var
  FindHandle: THandle;
  FindData: TWin32FindData;
begin
  Result := False;
  FindHandle := FindFirstFile(PChar(FileName), FindData);
  if FindHandle <> INVALID_HANDLE_VALUE then begin
    Result := (Bool(FindData.dwFileAttributes and FILE_ATTRIBUTE_REPARSE_POINT))
              and Bool(FindData.dwReserved0 and $80000000) // MS bit
              and Bool(FindData.dwReserved0 and $20000000) // name surrogate bit
              and (LoWord(FindData.dwReserved0) = 3); // mount point value
    winapi.windows.FindClose(FindHandle);
  end else
    RaiseLastOSError;
end;



回答2:


You can try also JCL (JEDI Code Library) JclNTFS unit.
it has a few methods to deal with junctions e.g:
NtfsIsFolderMountPoint / NtfsGetJunctionPointDestination.



来源:https://stackoverflow.com/questions/13382668/detecting-if-a-directory-is-a-junction-in-delphi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!