检查文件是真实文件还是符号链接
有没有办法使用 C# 来判断文件是真实文件还是符号链接?
我已经深入研究了 MSDN W32 文档 ,并且找不到任何内容来检查这一点。我在这里使用 CreateSymbolicLink,它工作正常。
Is there a way to tell using C# if a file is real or a symbolic link?
I've dug through the MSDN W32 docs, and can't find anything for checking this. I'm using CreateSymbolicLink from here, and it's working fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我有一些
符号链接的源代码发布在我的blog允许您:它还包含您可能希望扩展的 NUnit 测试用例。
重要的是:
即:
CreateFile()
DeviceIoControl()
来获取重解析点数据(注意:它可能是一个连接点!)I have some
source code for symlinks posted on my blogthat will allow you to:It also contains NUnit test cases, that you may wish to extend.
The meaty bit is:
That is:
CreateFile()
DeviceIoControl()
to get the reparse point data (NOTE: it could be a junction point!)从 .NET 6 开始,您可以使用:
FileSystemInfo.LinkTarget
属性属性说明:
例如:
Starting with .NET 6 you can use:
FileSystemInfo.LinkTarget
PropertyProperty description:
For example:
下面是区分文件和目录以及文件链接和目录链接的示例。
指向文件或目录的链接保留与其目标分开的自己的属性(创建日期、权限)。
可以删除文件链接(例如使用“del”)而不影响目标文件。
可以删除目录链接(例如“rmdir”)而不影响目标目录。使用“rd /s”时要小心。这将删除目录链接目标。
用于检查
FileInfo
和DirectoryInfo
的关键FileAttributes
标志是FileAttributes.ReparsePoint
。Here is an example of differentiating files and directories from links to files and links to directories.
Links to either files or directories maintain their own attributes (creation date, permissions) separate from their targets.
File links can be deleted (e.g. using "del") without affecting the target file.
Directory links can be removed (e.g. "rmdir") without affecting the target directory. Take care when using "rd /s". This WILL remove the directory link target.
The key
FileAttributes
flag to check in bothFileInfo
andDirectoryInfo
isFileAttributes.ReparsePoint
.事实证明上述答案并不可靠。
最后我从 MSDN:
It proves the above answers are not reliable.
Finally I got the right solution from MSDN:
GetFileInformationByHandle 填充 BY_HANDLE_FILE_INFORMATION 结构,其中有一个字段
dwFileAttributes
,其中位使用有关文件属性的信息进行设置(详细信息此处< /a>)。特别是,看看 mask 的部分......:GetFileInformationByHandle fills a BY_HANDLE_FILE_INFORMATION structure which has a field
dwFileAttributes
where bits are set with info about the file's attributes (details here). In particular, look at the bit at mask...:根据 这个答案 Stack Overflow 问题找出文件是否是 PowerShell 中的符号链接,获取System.IO.FileAttributes 文件(通过 File.GetAttributes),并测试ReparsePoint 位,有效。如果该位已设置,则它是符号链接或连接点。如果不是,则它是常规文件(或硬链接)。
According to this answer to Stack Overflow question Find out whether a file is a symbolic link in PowerShell, getting the System.IO.FileAttributes for the file (via File.GetAttributes), and testing for the ReparsePoint bit, works. If the bit is set, it is a symlink or a junction point. If not, it is a regular file (or hardlink).
库 MonoPosix 提供 API 来检查文件是否为符号链接:
The library MonoPosix provides API to check if a file is a symbolic link:
我知道我参加聚会迟到了,但在研究同样的问题时发现了这个讨论,
我发现下面的内容对我有用,所以我想我会发布以防其他人使用
它的工作原理如下:-
https://github.com/NCodeGroup/NCode.ReparsePoints https://www.nuget.org/packages/NCode.ReparsePoints/
I know I am late to the party but found this discussion when researching same question
I found the below worked for me so thought I would post in case of use to anyone else
It works like this:-
https://github.com/NCodeGroup/NCode.ReparsePoints https://www.nuget.org/packages/NCode.ReparsePoints/