【问题标题】:Copying Directory Not Letting Go Of Files复制目录不放文件
【发布时间】:2012-08-28 17:46:51
【问题描述】:

我直接从How to: Copy, Delete, and Move Files and Folders (C# Programming Guide) 得到这个代码:

string sourceDir = currDir + "\\" + "Trends";
string targetDir = currDir + "\\" + reportDir + "\\" + "Trends";
if (Directory.Exists(sourceDir))
{
    string[] files = Directory.GetFiles(sourceDir);

    foreach (string file in files)
    {
        string fileName = Path.GetFileName(file);
        string targetFile = Path.Combine(targetDir, fileName);
        File.Copy(file, targetFile, true);
    }
}

但是,当我运行此代码时,我会在 sourceDir 中的每个文件上抛出一个错误,因为它已经在使用中,所以无法访问它。当我退出代码时,很明显没有其他进程正在使用这些文件,所以一定是这段代码导致了问题。有没有办法为此使用“使用”?

感谢任何建议。

问候。

【问题讨论】:

  • 代码不运行时,是否可以手动复制文件?
  • 当代码没有运行时,我可以复制/移动/删除文件。
  • 什么程序创建文件?是同一个程序吗?
  • @BenVoigt:这些文件都是在运行时未生成的所有图像。它们是很久以前创建并保存到 sourceDir 的。
  • 您是否将您的reportdir 设置为.. ????

标签: c# file process


【解决方案1】:

感谢您提出的所有建议——他们让我找到了答案。我正在覆盖我的网页的 Render 方法以将其中一个表保存为 .html 文件。为了显示这个表,我需要 sourceDir 中的文件。但是,表格不会立即呈现,因此在第一次调用 Render 时,这些文件会被 Web 应用程序合法使用。我发布的代码运行良好。

【讨论】:

    猜你喜欢
    • 2018-01-10
    • 1970-01-01
    • 2018-05-19
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多