gpt4 book ai didi

c# - 复制目录不放过文件

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:18 24 4
gpt4 key购买 nike

我直接从 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 中的每个文件上都抛出一个错误,提示无法访问该文件,因为它已被使用。当我退出代码时,很明显没有其他进程正在使用这些文件,所以一定是这段代码导致了问题。有没有办法为此使用“使用”?

如有任何建议,我们将不胜感激。

问候。

最佳答案

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

关于c# - 复制目录不放过文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12164712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com