我看到以下代码片段出现了奇怪的错误:
File.Copy(oldPath, targetPath,true);
File.SetAttributes(targetPath, FileAttributes.Normal);
Run Code Online (Sandbox Code Playgroud)
必须将文件移动到其他位置,因为我在源路径上没有写入权限,所以我复制文件并设置目标文件的访问权限.在我的系统(Windows 7 SP1)上,这很好用.
但是,据我所知(在我所知)任何Windows 10机器上,程序在File.SetAttributes上崩溃并显示消息
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)
Run Code Online (Sandbox Code Playgroud)
这告诉我即使代码已经通过File.Copy()行,该文件尚未成功复制.File.Copy()不再同步工作,或者在这方面有不同的操作系统上有任何其他改变吗?
坦率地说,我很难过.起初我想到了计时问题并尝试在新线程中包装Copy调用,直到我读取File.Copy()在成功复制或运行成错误之前不会返回.