windows 检测符号链接、连接点、挂载点和硬链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2487237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:09:54  来源:igfitidea点击:

Detect Symbolic Links, Junction Points, Mount Points and Hard Links

c#.netwindowsfilesystemsreparsepoint

提问by Alexander

does anyone know how to check if a file or directory is either a Symbolic Link, Junction Point, Mount Pointor 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?

据我所知,通过检查文件的“ReparsePoint”属性来检测符号链接。通过检查“ReparsePoint”属性的目录来检测连接点。因此,如果在文件上设置了“ReparsePoint”属性,则它必须是符号链接,否则如果在目录上设置它,则它只能是连接点……对吗?

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?

到目前为止很好,但我仍然不知道如何检测“挂载点”和“硬链接”。谁能告诉我如何做到这一点?

采纳答案by Gabe

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.

符号链接、连接点和挂载点都是不同重解析点的示例。然而,硬链接只是普通文件。在 NTFS 上,所有文件都是硬链接。您可以检测到一个文件有多个指向它的硬链接,但没有它指向的“真实文件”。您可以将硬链接视为同一个文件的不同名称。

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

以下是有关从 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/

以下是有关如何在 C 中执行此操作的一些信息:http: //blog.kalmbach-software.de/2008/02/

回答by Hannes de Jager

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_INFORMATIONstructure contains the total number of links

您可以通过调用 Win32 API 函数GetFileInformationByHandle来检测多个名称是否指向相同的“数据块”或“文件内容” 。返回的BY_HANDLE_FILE_INFORMATION结构的nNumberOfLinks成员包含链接总数

Mount Points:

挂载点:

You can iterate through all the mount points on a volume using FindFirstVolumeMountPointand FindNextVolumeMountPoint. Also FindVolumeMountPointCloseshould be used to close the search handle.

您可以使用FindFirstVolumeMountPointFindNextVolumeMountPoint遍历卷上的所有挂载点。此外FindVolumeMountPointClose应该用来关闭搜索句柄。

From .NET

来自.NET

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

从 .NET 执行此操作需要一些 P/Invoke 魔法

回答by unixman83

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...

请参阅我的问题NTFS Junctions,理解 API 时遇到问题。这有点像问题的重复。但我解释了如何使用 C/C++ 实现重解析点、挂载点、连接点和符号链接。而不是仅仅提供指向 API 的链接,盲目地......