【问题标题】:Directory.Move results in directory is not empty errorDirectory.Move 导致目录不是空错误
【发布时间】:2017-03-18 21:05:22
【问题描述】:

我有一个在数百台服务器上运行的商业 ASP.NET 应用程序。当应用尝试移动通过 UNC 语法访问的文件共享上的目录时,一位客户遇到以下错误。

System.IO.IOException: The directory is not empty.

堆栈跟踪:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Directory.InternalMove(String sourceDirName, String destDirName, Boolean checkHost)
at GalleryServer.Business.AlbumSaveBehavior.PersistToFileSystemStore(IAlbum album)
at GalleryServer.Business.AlbumSaveBehavior.Save()
at GalleryServer.Business.GalleryObject.Save()
at GalleryServer.Business.Album.MoveTo(IAlbum destinationAlbum)
at GalleryServer.Web.Controller.AlbumController.TransferToAlbum(Int32 destinationAlbumId, GalleryItem[] itemsToTransfer, GalleryAssetTransferType  transferType, GalleryItem[] createdGalleryItems)
at GalleryServer.Web.Api.AlbumsController.TransferTo(Int32 destinationAlbumId, GalleryItem[] itemsToTransfer, GalleryAssetTransferType transferType)

代码如下(简化):

var sourceDir = @"\\server\storage\folder1";
var destDir = @"\\server\storage\folder2";

System.IO.Directory.Move(sourceDir, destDir);

我应该注意,当目录为空时,代码会成功。当它包含文件时它应该可以工作,并且确实可以为其他客户工作。

ASP.NET 应用程序正在 IIS 中运行,并使用应用程序池标识的 AD 帐户。我已确认它对文件共享目录具有“修改”权限。

什么样的服务器配置或权限条件会触发此错误?我自己进行了许多测试,但无法重现。

【问题讨论】:

  • 移动目录意味着删除原始目录及其文件。如果没有足够的权限,或者如果原始目录中的文件是“打开的”,那将会失败。或者也许只是其中的第二部分。
  • 它也可能是使用该文件的用户计算机上的防病毒软件...是否总是失败。如果您尝试使用资源管理器手动移动文件,您会得到什么?

标签: c# asp.net .net system.io.directory


【解决方案1】:

Directory.Move 内部调用 kernel32.dll 中的 MoveFilehttps://msdn.microsoft.com/en-us/library/windows/desktop/aa365239(v=vs.85).aspx

这会导致系统错误 0x91 (https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx)

从这里很难判断,但源文件夹中可能存在您的用户帐户无法移动的文件。

【讨论】:

    【解决方案2】:

    您可以试试here on SO 发布的答案吗?答案确实克服了内部功能的限制,并且更安全。

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多