如何以编程方式访问 Windows 符号链接的目标路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/221417/
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
How do I programmatically access the target path of a windows symbolic link?
提问by David Arno
Windows 6 (Vista and Server 2008) support proper symbolic links, which can be created via the CreateSymbolicLinkfunction. But there doesn't appear to be a corresponding function for interrogating a symbolic link to obtain the path of the link's target.
Windows 6(Vista 和 Server 2008)支持适当的符号链接,可以通过CreateSymbolicLink函数创建。但是似乎没有相应的函数来查询符号链接以获得链接目标的路径。
I have found out that symbolic links are an implementation of reparse points, and so the reparse point functions can be used to obtain the target path. But the header files that I need to use reparse points appear to come with the Windows Driver Kit. Setting up this kit with VS2008 appears to be a non trivial task.
我发现符号链接是重解析点的实现,因此可以使用重解析点函数来获取目标路径。但是我需要使用重新分析点的头文件似乎与Windows Driver Kit 一起提供。使用 VS2008 设置这个工具包似乎是一项不平凡的任务。
Is there a nice simple function that I've missed for obtaining a link's target, or do I really have to set up a windows driver development environment just to write code to access this information?
是否有一个很好的简单函数我错过了获取链接的目标,或者我真的必须设置一个 Windows 驱动程序开发环境来编写代码来访问这些信息?
EDIT: Adam Mitz came up with the suggestion of GetFinalPathNameByHandle. This function works just great for local symlinks, but doesn't appear to work for resolving remote links (via a UNC path).
编辑:Adam Mitz 提出了 GetFinalPathNameByHandle 的建议。此函数非常适用于本地符号链接,但似乎不适用于解析远程链接(通过 UNC 路径)。
EDIT 2: At Adam's request, here are more details of what I've tried:
编辑 2:应亚当的要求,这里是我尝试过的更多细节:
I initially went down the FSCTL_GET_REPARSE_POINT
/ DeviceIoControl
route, but that yields a REPARSE_DATA_BUFFER
structure. The headers that define this structure seem to exist solely within the Windows Driver Kit.
我最初沿着FSCTL_GET_REPARSE_POINT
/DeviceIoControl
路线走,但这会产生一个REPARSE_DATA_BUFFER
结构。定义此结构的头文件似乎只存在于 Windows 驱动程序工具包中。
GetFinalPathNameByHandle()
works fine when the link exists on a local disk (C:\...\link
etc). Curiously, I found that I could obtain the handle to the link - and thus get the target - using CreateFileW()
whether the FILE_FLAG_OPEN_REPARSE_POINT
flag was specified or not, regardless of whether the target file exists.
GetFinalPathNameByHandle()
当链接存在于本地磁盘(C:\...\link
等)上时工作正常。奇怪的是,我发现无论目标文件是否存在,我都可以获取链接的句柄 - 从而获取目标 - 使用CreateFileW()
是否FILE_FLAG_OPEN_REPARSE_POINT
指定了标志。
When CreateFileW()
and GetFinalPathNameByHandle()
are used to interrogate a remote link though (\\?\UNC\....
), things start to unravel. If FILE_FLAG_OPEN_REPARSE_POINT
is specified, GetFinalPathNameByHandle()
always returns the link path, not the target path. If FILE_FLAG_OPEN_REPARSE_POINT
is not specified, then the target path is returned, but only if the target exists and is on the same machine as the link. If the link points to another machine, I get a network permissions error. If the link points to a local - non-existent - file, I get a file not found error.
当CreateFileW()
和GetFinalPathNameByHandle()
被用来询问远程链接时 ( \\?\UNC\....
),事情开始变得混乱。如果FILE_FLAG_OPEN_REPARSE_POINT
指定,则GetFinalPathNameByHandle()
始终返回链接路径,而不是目标路径。如果FILE_FLAG_OPEN_REPARSE_POINT
未指定,则返回目标路径,但前提是目标存在且与链接位于同一台机器上。如果链接指向另一台机器,我会收到网络权限错误。如果链接指向一个本地不存在的文件,我会收到一个找不到文件的错误。
采纳答案by Adam Mitz
A final path is the path that is returned when a path is fully resolved. For example, for a symbolic link named "C:\tmp\mydir" that points to "D:\yourdir", the final filesystem path would be "D:\yourdir".
最终路径是完全解析路径时返回的路径。例如,对于指向“D:\yourdir”的名为“C:\tmp\mydir”的符号链接,最终的文件系统路径将是“D:\yourdir”。