3

I'm currently searching for a C# solution which can read the number of hardlinks of a file and their links locations. I'm thinking of the program link shell extension which can do this by going to the file properties and lists all the hardlinks of one file. I expect there is a solution for C# coders which can do the same without searching through the complete disk.

Edit 1:

Is there a way to test two HardLinks if they are the same file?

fpdragon
  • 1,867
  • 4
  • 25
  • 36
  • What kind of files are we talking about? Can you elaborate what you mean by "hardlink" in this case? – Jonathan Wood Dec 22 '10 at 14:42
  • Are you looking for a way to find and list all of the shortcuts to a particular file? Being able to do that without searching through the entire disk is filesystem dependent. – sourcenouveau Dec 22 '10 at 14:43
  • @Jonathan Wood: http://en.wikipedia.org/wiki/Hard_link – Jon Dec 22 '10 at 14:47
  • 1
    I'd say the word Hard Link is precise: A special type of reparse points under NTFS... the wiki link also tells it good. – fpdragon Dec 22 '10 at 14:51
  • Yes but are you referring to real hard links, or shortcuts (symbolic links)? Most Windows users don't even know that they can make hard links. – sourcenouveau Dec 22 '10 at 14:51
  • 1
    I am talking about hard links. No shortcuts and no symbolics links. By the way: shortcuts and symbolic links are not the same ;) – fpdragon Dec 22 '10 at 14:54
  • fpdragon: The term "hard link" is precise, but it does not mean a type of reparse point. A hard link is just another name for a given file. By asking how many links a file has, you're asking how many directory entries there are for it. – Gabe Dec 22 '10 at 15:40
  • Are you looking for the C# equivalent of running `fsutil hardlink list` on a file? – Gabe Dec 22 '10 at 15:43

1 Answers1

2

To get the link count, use GetFileInformationByHandle and access the NumberOfLinks member.

Here's an example of using the API in VB: http://blogs.msdn.com/b/vbteam/archive/2008/09/22/to-compare-two-filenames-lucian-wischik.aspx

To find the names of all the links, you need to use FindFirstFileNameW but it's only available as of Vista (or Server 2k8), and I can't find P/Invoke information for it.

Gabe
  • 84,912
  • 12
  • 139
  • 238