gpt4 book ai didi

c# - 如何删除除某些文件夹之外的所有文件夹

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

您好,我需要删除一个文件夹中的所有文件夹,但我卡住了,因为我不需要删除两个文件夹。我想做这样的事情:

string[] dir = Directory.GetDirectories(getDir());

foreach(string r in (dir.Where(x => x.Contains("AutomaticRecepes") == false))&&
dir.Where(y => y.Contains("SpecialRecepes") == false))
{
Directory.Delete(r, true);
}

如您所见,我想(使用几行代码)删除除名为 AutomaticRecepes 和 SpecialRecepes 的文件夹之外的所有文件夹。

我写的代码当然报错了:

operator && can not be applied to operand of type 'system.collections.generic.ienumerable' and 'system.collections.generic.ienumerable'

我该怎么做?

谢谢

最佳答案

您必须将条件更改为:

foreach(string r in dir.Where(x => x.Contains("AutomaticRecepes") == false && x.Contains("SpecialRecepes") == false))
{
Directory.Delete(r, true);
}

&& 用于连接 bool 表达式,但由于 Where 返回一个 IEnumerable,它会产生错误。

关于c# - 如何删除除某些文件夹之外的所有文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50872965/

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