Detect Symbolic Links, Junction Points, Mount Points and Hard Links [duplicate]

邮差的信 提交于 2019-11-27 17:53:27

问题


This question already has an answer here:

  • Check if a file is real or a symbolic link 6 answers

does anyone know how to check if a file or directory is either a Symbolic Link, Junction Point, Mount Point or Hard Link?

As far as I know a symbolic links are detected by checking a file for its "ReparsePoint" attribute. Junction points are detected by checking a directory for the "ReparsePoint" attribute. So if the "ReparsePoint" attribute is set on a file it must be a symbolic link, otherwise if it's set on a directory it can only be a junction point...right?

Good so far, but I have still no idea how to detect "Mount Points" and "Hard Links". Can anyone tell me how to do this?


回答1:


Symbolic Links, Junction Points, and Mount Points are all examples of different reparse points. Hard Links, however, are just regular files. On NTFS all files are hard links. You can detect that a file has multiple hard links pointing to it, but there's no "real file" that it points to. You can think of hard links as just different names for the same file.

Here's some information on accessing reparse points from C#: http://www.codeproject.com/KB/vista/ReparsePointID.aspx?display=Print

Here's some information on how to do it in C: http://blog.kalmbach-software.de/2008/02/




回答2:


Hard links:

You can detect if multiple names are pointing to the same "data chunk" or "file content" by invoking the Win32 API function GetFileInformationByHandle. The nNumberOfLinks member of the returned BY_HANDLE_FILE_INFORMATION structure contains the total number of links

Mount Points:

You can iterate through all the mount points on a volume using FindFirstVolumeMountPoint and FindNextVolumeMountPoint. Also FindVolumeMountPointClose should be used to close the search handle.

From .NET

Doing this from .NET will require some P/Invoke magic




回答3:


Please see my question NTFS Junctions, trouble understanding the API. It is kind of a duplicate of the question. But I explain all about how reparse points, mountpoints, junctions, and symbolic links are implemented, using C/C++. Instead of just giving links to API, blindly...



来源:https://stackoverflow.com/questions/2487237/detect-symbolic-links-junction-points-mount-points-and-hard-links

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