确定文件是否是联结(在Windows中)? [英] Determine whether a file is a junction (in Windows) or not?

查看:53
本文介绍了确定文件是否是联结(在Windows中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方法来确定文件是否是结点,但没有找到令人满意的答案.

I've been searching around trying to find a way to determine if a file is a junction or not, and have not found any satisfactory answers.

我尝试的第一件事是:

Files.isSymbolicLink(aPath)

它仅检测符号链接,而不检测Windows中称为联结的文件.

It detects only symbolic links not the files referred to as junctions in Windows.

还尝试了此处提出的解决方案(使用JNA库): Stackoverflow问题(3249117) ,但对于我知道是结点的任何文件,它从未返回true.

Also tried the solution proposed here (using JNA library): Stackoverflow question (3249117) , but it never returned true on any of the files I know to be junctions.

我发现确定哪些文件是联结的唯一方法是在Windows命令提示符下运行以下命令:

The only way I've found to determine which files are junctions is the following command run in windows command prompt:

DIR /S /A:L

在我的计算机上,它返回66个文件夹,而wheras Files.isSymbolicLink(aPath)仅返回2. 因此,我想我可以找到一种利用此方法的方法,但我认为遍历文件树时效率不高.

On my computer it returns 66 folders, wheras Files.isSymbolicLink(aPath) returned only 2. So I suppose I could find a way to utilize this, but I don't think it would be very effiecient when traversing a filetree.

是否可以使用标准Java库或JNA来实现此目的?

Is there any way to do this using the standard java library, or alternativly JNA?

推荐答案

如果可以用JNA编写本机代码,则可以直接调用Win32 API GetFileAttributes()函数并检查FILE_ATTRIBUTE_REPARSE_POINT标志(连接实现为重新解析点).

If you can write native code in JNA, you can directly call the Win32 API GetFileAttributes() function and check for the FILE_ATTRIBUTE_REPARSE_POINT flag (junctions are implemented as reparse points).

更新:要区分不同类型的重解析点,必须获取实际重解析点的ReparseTag.对于结点,它将设置为IO_REPARSE_TAG_MOUNT_POINT(0xA0000003).

Update: To differentiate between different types of reparse points, you have to retreive the ReparseTag of the actual reparse point. For a junction point, it will be set to IO_REPARSE_TAG_MOUNT_POINT (0xA0000003).

有两种方法可以恢复ReparseTag:

  1. DeviceIoControl() FSCTL_GET_REPARSE_POINT 控制代码以获得

  1. Use DeviceIoControl() with the FSCTL_GET_REPARSE_POINT control code to obtain an REPARSE_DATA_BUFFER struct, which as a ReparseTag field. You can see an example of an IsDirectoryJunction() implementation using this technique in the following article:

NTFS硬链接,目录连接和Windows快捷方式

使用 FindFirstFile() 来获取 WIN32_FIND_DATA 结构.如果路径具有FILE_ATTRIBUTE_REPARSE_POINT属性,则dwReserved0字段将包含ReparseTag.

Use FindFirstFile() to obtain a WIN32_FIND_DATA struct. If the path has the FILE_ATTRIBUTE_REPARSE_POINT attribute, the dwReserved0 field will contain the ReparseTag.

这篇关于确定文件是否是联结(在Windows中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆