File.Move在前面有File.Delete时失败 [英] File.Move fails when preceded by a File.Delete

查看:331
本文介绍了File.Move在前面有File.Delete时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个MoveFile方法,通常可以使用,但在客户站点仍然失败。

We have a MoveFile method which usually work, but keep failing at a customer site.

if (File.Exists(target))
{
    File.Delete(target);
}

File.Move(source, target);

File.Move 的调用反复失败with

The call to File.Move fails repeatedly with


System.IO.IOException: Cannot create a file when that file already exists.

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.__Error.WinIOError()
   at System.IO.File.Move(String sourceFileName, String destFileName)


我们有周围调用的错误处理那个方法,但是我们无法弄清楚为什么 File.Delete 不起作用而且没有抛出任何东西。

We have error handling surrounding call to that method, but we can't figure out why File.Delete is not working and is not throwing anything.

我们虽然关于文件权限,但是 File.Delete 会抛出 UnauthorizedAccessException

We though about file permission, but then the File.Delete would have throw an UnauthorizedAccessException.

是否有任何其他原因会导致 File.Move 在文件已存在之前失败那个特定的文件?

Are there any other reason that would make File.Move fail with a "file already exist" when it is preceded by the deletion of that specific file?

推荐答案

你能扭转逻辑吗?

File.Copy (source, target, true) 

要覆盖然后目标

File.Delete(source)

这篇关于File.Move在前面有File.Delete时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆