1

I am trying to programmatically delete a broken symbolic link and replace it with one that is has a valid target by the same name. However, by the time CreateSymbolicLink() executes, File.Delete() still has not actually deleted the file (I have verified the file still exists after File.Delete() executes, with no errors or warnings).

When the program finishes executing, only then does the file actually get deleted. What the heck? This is blocking the symbolic link from being created. Any ideas what to do?

    private static string replaceSymbolicLink(string linkPath, string newTargetPath)
    {
        Boolean linkIsFile = File.Exists(linkPath);
        Boolean linkIsDir = Directory.Exists(linkPath);

        // Create a replacement of the same name and link type with the new target
        string newTargetPathDOS = getAbsPathFromPath(newTargetPath);
        Boolean targetIsFile = File.Exists(newTargetPathDOS);
        Boolean targetIsDir = Directory.Exists(newTargetPathDOS);
        if (targetIsFile || targetIsDir)
        {
            if (linkIsFile)
            {
                File.Delete(linkPath);
                Console.WriteLine(File.Exists(linkPath));
            }
            else
            {
                Directory.Delete(linkPath);
            }
            SymbolicLink type;
            if (targetIsFile)
            {
                type = SymbolicLink.File;
            }
            else
            {
                type = SymbolicLink.Directory;
            }
            CreateSymbolicLink(linkPath, newTargetPath, type);
            return getGuidFromPath(linkPath);
        }
        return null;
    }
2
  • 1
    Could you paste your code and an example of what you are trying to do?
    – Rush
    Mar 29, 2015 at 17:15
  • I've had issues with File.Delete / Move / Copy calls that complete asynchronously behind the scenes. For successive interdependent operations I've even resorted to Thread.Sleep(500) between calls, which feels dirty but has done the trick. Would like to know a better solution.
    – yoyo
    Mar 29, 2015 at 17:46

1 Answer 1

4

On Windows deletions are not immediate. Files are being marked as deleted and actually disappear when the last handle is being closed. Normally, this mechanism is not visible since most files are opened with sharing rights that prevent deletion. This causes deletes to either fail or go through immediately.

FileShare.Delete allows to keep a file (a FileStream) open while it is being deleted. This can be surprising behavior if you have never heard of this before.

So you probably have the file still open or some other process has.

7
  • The code never opens the file (unless it opens it for the purpose of deleting it). I already know it's not being deleted, I need a suggestion about how to force it to be deleted before the delete command finishes.
    – Wes
    Mar 29, 2015 at 17:21
  • There is no way to force anything. I have described to you how the system works and you have to work with that system. Suggestion: Put a breakpoint after the Delete call. Then, use Process Explorer's search to make sure that there are no open handles. (There will be one. Or, it is a race and the handle has disappeared before you see it.); You can use Process Explorer to track all operations on a given path. There, you will find that deletions are an open-mark-close sequence.
    – usr
    Mar 29, 2015 at 17:25
  • 1
    A potential workaround would be to rename the file before deleting to a temporary name (Guid.NewGuid). Maybe renames are allowed at that point in time and the delayed deletion no longer prevents you from reusing the file name.
    – usr
    Mar 29, 2015 at 17:27
  • 1
    OK I owe you an apology, I found a stray open handle in my C++ custom DLL and closing it seems to have solved the problem.
    – Wes
    Mar 29, 2015 at 17:33
  • 1
    Don't open the file with FILE_SHARE_DELETE or whatever it is called. This behavior is opt-in. I believe it is for creating temporary self-destroying files and continuing to use them.
    – usr
    Mar 29, 2015 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.