我想确保我要移动的文件在它的目的地不存在。这就是我要做的
// delete if exists already
if (File.Exists(target))
{
File.Delete(target);
}
// move to target
File.Move(source, target);
但有时,当程序点击file . move时,文件并没有完全删除。为了解决这个问题,我计划在成功删除文件后使用FileSystemWatcher来恢复流。但是我不想一直等待,所以我想在FileSystemWatcher上设置一个超时,以便在一段时间后恢复流,即使文件没有被删除。
我想把这个超时设置为文件大小的函数。假设文件大小为1MB,超时时间是1秒如果文件大小为10MB,超时时间是10秒。有人知道选择超时的最佳方法吗?或者我应该使用一个固定的超时,不管文件的大小。
反过来做:
// passing true as the 3rd param to Copy() causes it to overwrite the target file
File.Copy(source, target, true);
File.Delete(source);
(如果有File.Move(string, string, bool)
,我会说使用它,但没有)