1

事实证明,这很难调试。先从我的情况说起:

我有一个使用 .Net 4 使用 C# 开发的 ASP.Net MVC3 Web 应用程序。系统的一部分允许用户上传 zip 文件。这样做很好,并保存了 zip 文件。还有一个 Windows 服务会定期查找新的 zip 文件,解压缩它们,做一些工作,然后重新压缩它们。(我System.IO.Compression用于拉链的东西)。这部分一切正常,在处理后我最终得到了一个类似的结构。

Object1Folder
 \_ Input.zip
 \_ ExtractedFolder
 \_ Output.zip

还有一个功能允许用户删除项目,要求是删除对象文件夹,在本例中为“Object1Folder”。因为我需要删除所有子文件夹和文件,所以我有以下递归函数来完成这项工作......

public static void DeleteDirectory(string directoryPath)
{
    string[] files = Directory.GetFiles(directoryPath);
    string[] directories = Directory.GetDirectories(directoryPath);

    foreach (string file in files)
    {
        File.SetAttributes(file, FileAttributes.Normal);
        File.Delete(file);
    }

    foreach (string directory in directories)
    {
        DeleteDirectory(directory);
    }

    Directory.Delete(directoryPath, true);
}

最初被称为...

DeleteDirectory("C:\\Objects\\Object1Folder");

但它不起作用!首先,没有抛出错误,代码似乎成功执行,这很烦人。但结果是只删除了“Input.zip”文件。“ExtractedFolder”和“Output.zip”文件仍然存在。

据我所知,代码是正确的。我看不出它没有尝试删除剩余的文件和文件夹。不幸的是,我们没有在目标服务器上安装 VS,所以我无法单步执行代码并检查它是否试图删除它们。请指出您是否可以看到这是一个潜在的问题?

到目前为止,我最好的猜测是这是某种权限问题。有趣的是(也许)是当我去手动清理问题时(即Windows资源管理器删除“Object1Folder”它说“Denied”并要求我用管理员权限按钮确认它所做的事情。

我知道你们很难解决这个问题,但我正在寻找我应该检查的东西来尝试解决这个问题。我应该确保哪些事情具有正确的权限?他们需要什么权限?有什么好方法可以让我调试这个问题吗?如果其他东西持有这些文件(可能来自 Windows 服务的提取过程),我如何检查这是否是问题所在?

有关该服务器的一些信息可能会有所帮助:它正在运行带有 Service Pack 1 的“Windows Server 2008 R2 Datacenter”,并且是 64 位的。Web 应用程序分配了一个用户,该用户同时是“用户”和“IIS_IUSRS”的成员。

如果你们中的任何人需要一些额外的信息,请告诉我。

4

2 回答 2

1

查看服务器上的事件日志;您可能会在那里找到异常\错误消息。

You could consider using Directory.Delete(path, true) so you delete the folder and all its content in one call (unless I do not understand your code correctly).

Have a look at files being in use. If a file is in use, the OS can't delete it. So make sure you are releasing all files correctly.

Finally, you can't force the files to be not in use so you might end up writing a clean script that runs every night to delete unwanted files and folders.

于 2013-01-11T10:54:23.627 回答
0

You really should use a temp destination folder for your unpacked files. When they have been used you can try deleting them and if that fails just leave them. The permission to write to a non-temp folder should not really be given to a presentation app residing in the iis.

于 2013-01-11T15:53:45.440 回答