列出文件的硬链接(C# 中)
我想编写一个程序来显示另一个带有硬链接的驱动器的文件。
我想保持两个硬链接在文件名和其他方面的一致,所以我必须获得一个函数/方法,在其中可以列出文件的所有当前硬链接。
例如:
我有一个文件 C:\file.txt
和另一个指向 D:\file.txt
的硬链接。
然后我将 D:\file.txt
重命名为 D:\file_new.txt
。
我现在希望也能够重命名 C 驱动器上的硬链接。
因此,我需要一个返回 D:\file_new.txt
的函数,其中存在以下硬链接:
C:\file.txt
D:\file_new.txt
然后我可以重命名 C:\
上的硬链接也获得 < code>D:\file_new.txt
所以我需要获取物理文件的所有硬链接。 或者:使用硬链接寻址的文件的所有硬链接。
希望有人可以帮忙!
编辑:
奥利弗注意到硬链接不能在不同的磁盘上使用。谢谢...所以我将问题延伸为:我需要什么?连接点?符号链接?它还应该适用于文件,而不仅仅是文件夹!
I want to write a program that shows the files of another drive with hard links.
I want to keep both hardlinks consistent in filename and other things, so I have to get a function/method where I can list all current hard links of a file.
For example:
I have a file C:\file.txt
and a second hard link to D:\file.txt
.
Then I rename D:\file.txt
to D:\file_new.txt
.
I now want to be able to also rename the hardlink on the C drive as well.
So I need a function which returns for D:\file_new.txt
that there are the following hardlinks:
C:\file.txt
D:\file_new.txt
then I can rename the hard link on C:\
also to get D:\file_new.txt
So I need to get all hard links of a physical file.
Or: All hard links of a file addressed with a hard link.
Hope somebody can help!
Edit:
Oliver noticed that hard links can't be used on different disks. thanks... So I extend the question to: What do I need? Junction Points? Symbolic Links? It should also work with files not only with folders!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
以下代码应该可以正常工作(最初由 Peter provost 在 PowerShell Code Repository 上发布):
the following code should work well (originally postet by Peter provost on PowerShell Code Repository):
也许我误解了你的问题,但硬链接不能从一个驱动器转到另一个驱动器。它们只能存在于单个驱动器上。
在 .Net 框架内,不支持获取这些信息。但 Win32 API 可以为您提供这些信息。
请查看这篇文章。它可能对你有帮助。
更新
据我所知,不可能在不同的驱动器之间执行此操作。连接点绝对不是你的朋友,因为它只适用于折叠。但在阅读这篇维基百科文章之后,您似乎可以在 Vista 和 Win7 上使用符号链接来完成此操作。还有一个指向 此 shell 扩展 的链接,它似乎涵盖了您可以执行的所有操作具有这些 NTFS 特殊功能。也许通过这个你可以检查你的目标是否可以实现,然后也许可以检查 MSDN 以获得所需的 Win32 API 函数。
Maybe i misunderstand your questions, but hardlinks can't go from one drive to another. They can only exist on a single drive.
Within the .Net framwork there is no support to get these informations. But the Win32 API can provide you with these informations.
Take a look at this article. It may help you.
Update
As far as i know it is not possible to do it between different drives. Junction Points are definitely not your friend cause it only works on foldes. But after reading this wikipedia article it seems that you can do it on Vista and Win7 with symbolic links. There is also a link to this shell extension which seems to cover everything you can do with these NTFS special features. Maybe with this you can check if your goal is reachable and maybe afterwards check the MSDN for the desired Win32 API function.
注意:
硬链接只能是同一卷上的文件,这与问题的要求相矛盾,从而导致了以下问题问题正文中的问题OP本人回答了。
但是,鉴于问题的标题,通过谷歌搜索找到这篇文章的用户很可能对问题的解决方案感兴趣如标题中所述:给出文件,我如何找到它的所有硬链接(根据定义,它们都在同一卷上)。
下面的解决方案是Marcel Nolte 的有用答案的简化和现代化改编。< /p>
它的行为和约束是:
对于给定的输入文件,其硬链接数组作为完整文件路径返回,其中包括输入文件的路径本身。
如果文件只有一个硬链接(本身),或者您指定了一个目录,则仅返回该文件/目录的完整路径。
如果路径引用的卷不支持硬链接,或者路径不存在,则返回
null
。以下是一个独立的 Windows 控制台应用程序,您应该能够按原样编译和运行它;感兴趣的方法是HardLinkHelper.GetHardLinks():
Note:
Hard links can only be files on the same volume, which contradicts the requirements of the question, which led to a follow-up question in the question body that the OP himself answered.
Given the title of the question, however, users who find this post by googling are most likely interest in a solution to the problem as stated in the title: given a file, how can I find all hard links to it (which are by definition all on the same volume).
The solution below is a streamlined and modernized adaptation of Marcel Nolte's helpful answer.
Its behavior and constraints are:
For a given input file, its array of hard links is returned, as full file paths, which includes the input file's path itself.
If the file has only one hard link (itself), or you specify a directory, only that file's / directory's full path is returned.
If the path refers to a volume that doesn't support hard links, or the path doesn't exist,
null
is returned.The following is a self-contained Windows console application that you should be able to compile and run as-is; the method of interest is
HardLinkHelper.GetHardLinks()
:我找到了一个解决方案:
首先,我不必使用硬链接(因为它们不能指向其他磁盘)。我必须改用符号链接。因此,我在原始磁盘上有一个硬链接文件,在其他磁盘上有指向该文件的符号链接。限制是操作系统必须是 Vista 或更高版本。
其次,我必须能够找出符号链接指向的位置。
在这里我找到了一个很好的例子如何找到我需要的信息:
http://www.codeproject.com/KB/vista/ReparsePointID.aspx
我唯一没有做到的是从特定文件(硬链接)中查找所有符号链接。我想没有现成的解决方案,我必须递归所有符号链接并测试目标。但就我而言,这没有问题。
我希望这可以帮助其他人!
I found a solution:
First I don't have to use hard links (since they can't point to an other disk). I have to use symbolic links instead. So I have one hard linked file on the original disk and symbolic links on other disks to this file. The limitation is OS must be Vista or newer.
Second I have to be able to find out where the symbolic link is pointing to.
Here I found a good example how to find out the information I need:
http://www.codeproject.com/KB/vista/ReparsePointID.aspx
The only thing I don't managed is to find all symbolic links from a specific file (hard link). I guess there is no out of the box solution and I have to recurse all symbolic links and test the target. But in my case that's no problem.
I hope that can help others!
我在项目中使用了 HardLinkHelper 类,它非常适合在 Windows NTFS 驱动器上查找硬链接。
这是我的 HardLinkHelper 类版本,进行了以下更改:
用法示例:
foreach(HardLinkHelper.GetHardLinks(entry.Path,true)中的var链接)
I used the HardLinkHelper class in a project, and it works great for finding hard links on Windows NTFS drives.
Here's my version of HardLinkHelper class with the following changes:
Example usage:
foreach (var link in HardLinkHelper.GetHardLinks(entry.Path, true))
尝试:
并循环遍历数组,找到文件并更改名称
编辑:
通过阅读评论,我知道我在知道什么是硬链接之前就回答了。我现在意识到这个答案没有帮助。
try:
and loop through the arrays, find the files and change the name
EDIT:
By reading the comments I know that I answered before I knew what a hardlink is. I realise now that this answer is not helping.