File.Delete(x) works where File.Copy(y, x, true) would fail

  .net-3.5, c++, copy, windows

I was trying to copy an existing file to another local path, where a file with the same file might already exist. So this is was my first option:

File.Copy(outputFileFullName, newFileName, true);

On some occasions, if newFileName already exists, this fails with UnauthorizedAccessException trying to access the existing file.
Ok then, what I get is that for some reason the program has no permission to delete the existing file.

But, much to my surprise, on the same files this code works and no exception is thrown:

if (File.Exists(newFileName))
      File.Delete(newFileName);
    
File.Copy(outputFileFullName, newFileName, true);

What is File.Delete(x) doing differently than File.Copy(y, x, true) in terms of deleting the existing file?

Source: Windows Questions

LEAVE A COMMENT