c# - C#:foreach语句中Directory.GetFiles的副作用并删除文件?

标签 c# file-io

我有以下foreach循环:

using System.IO;
//...   
if (Directory.Exists(path))
{
    foreach(string strFile in Directory.GetFiles(path, "*.txt"))
    {
        // do something, possibly delete the file named strFile
    }
}


删除当前用于foreach循环的目录中的文件时,会有副作用吗?

最佳答案

GetFiles返回一个数组,而不是一个迭代器,因此在您引用第一个文件时该操作已完成。而且它仅返回文件名,而不返回文件句柄,因此您应该完全安全地对其执行任何操作。

关于c# - C#:foreach语句中Directory.GetFiles的副作用并删除文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/322772/

相关文章:

c++ - 从二进制文件中逐个读取字节

asp.net - 当图像存储在 Web 根目录之外时如何在页面上显示图像

java - 并行数组 : how to initialize the length of the arrays when the input file is unknown

c# - 使用字符串变量设置 'Class = new <string of Class()>'

c# - 如何在asp.net web api中使用caSTLe Windsor在身份验证过滤器属性中实现依赖注入(inject)?

c# - 创建社区驱动的网站需要哪些步骤?

C# 将数组或列表添加到列表中

c# - 如何并行处理项目,然后合并结果?

python - 使用 python 以相同的速率迭代文本文件中的列表和行?

java - 读取 .txt 文件的内容并将其显示在多个文本区域