c# - 如果文件列表中缺少一个文件,如何评估为 false

标签 c#

我正在尝试检查一个文件夹中是否有一堆不同的文件。我已将文件添加到列表中并循环浏览。 问题是如果其中一个文件丢失,我不知道如何评估为 false。

只有当所有文件都存在时,我才需要 bool 评估为真。

    static List<string> files = new List<string>();
    static bool fileExists;
    static string Dir = @"Z:\MyDir\myFolder\";

    static void Main(string[] args)
    {
        files.Add("MyFile1.txt");
        files.Add("MyFile2.txt");
        files.Add("MyFile3.txt");
        files.Add("MyFile4.txt");
        files.Add("MyFile5.txt");
        files.Add("MyFile6.txt");
        files.Add("MyFile7.txt");
        files.Add("MyFile8.txt");
        files.Add("MyFile9.txt");
        files.Add("MyFile10.txt");

        foreach(string fl in files)
        {
            fileExists = (File.Exists(Dir + fl));   
        }

最佳答案

类似于 Linq 解决方案,但这是为老派爱好者准备的....

    fileExists = true;
    foreach(string fl in files)
    {
        if(!File.Exists(Path.Combine(Dir,fl)))
        {
           fileExists = false;
           break;
        }
    }

关于c# - 如果文件列表中缺少一个文件,如何评估为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20601057/

相关文章:

c# - 如何实现简单的字符串搜索

C#使用like运算符的数据表过滤器

c# - C# 与 LISP 中的函数式编程

c# - 如何在文件的一部分上使用 DeflateStream?

c# - ntohs() 和 ntohl() 等效?

c# - 使用 XMLA 或 C# 在 Analysis Services 中配置主动缓存

c# - MySql 奇怪(对我来说)where 子句的性能

c# - SQLiteDataAdapter 更新方法返回 0

c# - 从 C# 打印数据的好方法?

c# - WinStore : Slide a Grid on- and off-screen