I am seeing weird errors with the following code snippet:
File.Copy(oldPath, targetPath,true);
File.SetAttributes(targetPath, FileAttributes.Normal);
A file has to be moved somewhere else, and because I lack write right at the source path, I copy the file and set access rights for the destination file. On my system (Windows 7 SP1) this works fine.
However, on (as far as I know) any Windows 10 machine the program crashes at File.SetAttributes with the message
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: Could not find file 'C:\ProgramData\...\BlankDb.sdf'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.SetAttributes(String path, FileAttributes fileAttributes)
This tells me even though the code has passed the File.Copy() line, the file has not yet been copied successfully. Does File.Copy() not work synchronously anymore, or has anything else changed on differing OS in this regard?
Quite frankly, I am stumped. At first I thought of timing issues and tried wrapping the Copy call in a new Thread, until I read the File.Copy() will not return anyways before copying successfully or running into an error.