Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,124 | Comments: 45,475

filter by tags archive

Deleting hard links problem

time to read 2 min | 214 words

This code fails:

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes);

static void Main()
    var writer = File.CreateText("test.txt");

    CreateHardLink("hard_link.txt", "test.txt", IntPtr.Zero);



The problem here is that the hard_link.txt file is considered to be open. What I would expect to happen is that the delete would succeed in removing the directory entry, but keep the file. In other words, I would only expect it to fail if this is the last directory entry for the file (resulting in an actual delete).

For now I changed this to try to delete, but on failure, mark the file to be deletes on the next reboot.


Steven Kimpe


It's probably not "hard_link.txt" that's left open, but rather "text.txt"

This will probably do the trick.


using(var writer = File.CreateText("test.txt"))





CreateHardLink("hard_link.txt", "test.txt", IntPtr.Zero);



Yeah, +1 for Steven. That should work fine.

Ayende Rahien


That is pretty much the whole point.

Deleting a hard link isn't actually deleting the file, it is removing the metadata, it should not fail.

Steven Kimpe

Hi Ayende,

I agree completely with you and I find it counter intuitive as well... it's just a different way to tackle the problem.

Bruno Martinez

A better workaround is to open the writer like this

var writer = new StreamWriter(File.Open("test.txt", FileMode.Create, FileAccess.Write, FileShare.Delete));

The downside is that you allow test.txt to be deleted too.

Ayende Rahien


I don't actually control opening that file

Steve Py

Hi Ayende,

I think the trouble is that you're using File.Delete() to try and delete the HardLink when it's really trying you delete the underlying file.

Use the kernel's DeleteFile() method and it should delete the hardlink without the error.

Ayende Rahien


Nope, I am trying to delete the hard link, I don't care bout the file.

And File.Delete resolves down to Win32 DeleteFile

Steve Py

Hmm, well it doesn't throw the exception, but I see it doesn't delete the link file directory entry either so long as a handle to the original is in use.

Ayende Rahien


Did you check the return code and GetLastError()?


Ayende, are you ok? You haven't posted in 4 days...


Ayende, i`ve got that from msdn:

"(...)Flags, attributes, access, and sharing that are specified in CreateFile operate on a per-file basis. That is, if you open a file that does not allow sharing, another application cannot share the file by creating a new hard link to the file.(...)"

src: msdn.microsoft.com/.../aa363860%28VS.85%29.aspx

Comment preview

Comments have been closed on this topic.


  1. The design of RavenDB 4.0: Making Lucene reliable - 2 hours from now
  2. RavenDB 3.5 whirl wind tour: I’ll find who is taking my I/O bandwidth and they SHALL pay - about one day from now
  3. The design of RavenDB 4.0: Physically segregating collections - 2 days from now
  4. RavenDB 3.5 Whirlwind tour: I need to be free to explore my data - 3 days from now
  5. RavenDB 3.5 whirl wind tour: I'll have the 3+1 goodies to go, please - 6 days from now

And 13 more posts are pending...

There are posts all the way to May 30, 2016


  1. RavenDB 3.5 whirl wind tour (14):
    02 May 2016 - You want all the data, you can’t handle all the data
  2. The design of RavenDB 4.0 (13):
    28 Apr 2016 - The implications of the blittable format
  3. Tasks for the new comer (2):
    15 Apr 2016 - Quartz.NET with RavenDB
  4. Code through the looking glass (5):
    18 Mar 2016 - And a linear search to rule them
  5. Find the bug (8):
    29 Feb 2016 - When you can't rely on your own identity
View all series



Main feed Feed Stats
Comments feed   Comments Feed Stats