Skip to content

Commit

Permalink
File.Exists() returns false for symlinks to directories
Browse files Browse the repository at this point in the history
Use a different approach to avoid exceptions trying to create a
symlink that already exists.
  • Loading branch information
jstedfast committed Mar 2, 2023
1 parent 3e4b022 commit 64b528d
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,29 @@ private static void CreateSymbolicLinks(string targetDir, string sourceDir)
{
var target = Path.Combine(targetDir, Path.GetFileName(entry));

if (File.Exists(target))
var fileInfo = new FileInfo(target);
if (fileInfo.Exists)
{
if (fileInfo.LinkTarget is not null && fileInfo.LinkTarget.Equals(entry, StringComparison.Ordinal))
{
continue;
}

File.Delete(target);
}
else
{
var dirInfo = new DirectoryInfo(target);
if (dirInfo.Exists)
{
if (dirInfo.LinkTarget is not null && dirInfo.LinkTarget.Equals(entry, StringComparison.Ordinal))
{
continue;
}

Directory.Delete(target);
}
}

File.CreateSymbolicLink(target, entry);
}
Expand Down

0 comments on commit 64b528d

Please sign in to comment.