c#将文件名保存到aa中类型是Liststring然后检索一个文件夹下面的所有文件名如何排除aa中已经存在的文件
可以使用以下代码将文件夹下的所有文件名保存到 aa 中,并排除已经存在的文件:
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main()
{
string folderPath = "your_folder_path";
List<string> aa = new List<string>();
// 添加已存在的文件名到 aa
aa.AddRange(Directory.GetFiles(folderPath).Select(Path.GetFileName));
// 添加新的文件名到 aa
aa.AddRange(Directory.GetFiles(folderPath).Select(Path.GetFileName).Except(aa));
// 输出 aa 中的文件名
foreach (string fileName in aa)
{
Console.WriteLine(fileName);
}
}
}
其中,folderPath 是要检索的文件夹路径。首先,将已存在的文件名添加到 aa 中,然后再将新的文件名添加到 aa 中。最后,使用 foreach 循环输出 aa 中的文件名
AI 优化版
原文地址:https://www.cveoy.top/t/topic/hJ0q 著作权归作者所有。请勿转载和采集!