Search code examples
c#hardlinkjunction

List Hard Links of a file (in C#)


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!


Solution

  • 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!