C#类检查文件夹中是否存在文件 [英] C# class to check if files exists in folder

查看:540
本文介绍了C#类检查文件夹中是否存在文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,我写了以下课程:

I am very new to C# and i have written the following class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PrinterManager
{
    class CheckFilesExist
    {
        public class CheckFilesExist
        {
            public static bool check(bool isThere)
            {
                DirectoryInfo di = new DirectoryInfo("c:\temp");
                FileInfo[] TXTFiles = di.GetFiles("*.xml");
                if (TXTFiles.Length == 0)
                {
                    return isThere;
                }
                foreach (var fi in TXTFiles)
                    //return (fi.Exists);
                    return (fi.Exists);

                    return check;
            }

        }

    }
}

它应该检查目录中的* .xml文件并将其添加到数组中。如果找不到任何文件,则应返回false。它将通过数组并根据检查返回true或false。

It should check the directory for *.xml files and add them to an array. If it doesnt find any files then it should return false. It will go through the array and return true or false based on "check".

但是我遇到以下错误:


无法将方法组 check转换为非委托类型 bool。您打算调用
吗?

Cannot convert method group 'check' to non-delegate type 'bool'. Did you intend to invoke the method?

它似乎并不喜欢返回检查;最后,

It doesnt seem to like, "return check;" at the end.

我基本上想返回检查以查看文件夹中是否存在文件。
有什么想法为什么我的行不通吗?

I basically want to return a check to see if files exists in the folder. Any ideas why mine isnt working?

谢谢

推荐答案

不,它不喜欢退回支票; 最后-您希望它做什么?您的方法 check -因此,根据 check 返回正确或错误不会真的很有意义。您实际上是 mean 来返回 isThere 吗?

No, it doesn't like return check; at the end - what did you want it to do? Your method is check - so "return true or false based on check" doesn't really make sense. Did you actually mean to return isThere instead?

目前尚不清楚参数是什么说实话,它甚至真的意味着存在...并且,如果您已向 DirectoryInfo 请求该目录中的文件,我个人希望它们能够存在-否则为什么会返回它们?

It's not clear what the parameter is even really meant to be there for, to be honest... and if you've asked a DirectoryInfo for the files in that directory, I'd personally expect them to exist - otherwise why would they be returned?

哦,您的目录名称包含一个标签,我怀疑您实际上想要反斜杠后跟一个 t

Oh, and your directory name contains a tab where I suspect you actually want a backslash followed by a t.

我怀疑您的方法会更好:

I suspect your method would be better as:

public static bool TempDirectoryContainsXmlFiles()
{
    DirectoryInfo di = new DirectoryInfo(@"c:\temp");
    return di.GetFiles("*.xml").Length > 0; 
}

或使用LINQ为清楚起见:

or using LINQ for clarity:

public static bool TempDirectoryContainsXmlFiles()
{
    DirectoryInfo di = new DirectoryInfo(@"c:\temp");
    return di.GetFiles("*.xml").Any();
}

(您可以使用 EnumerateFiles ,如BrokenGlass所示,对于大型目录而言,效率更高,但仅在.NET 4及更高版本中可用。)

(You can use EnumerateFiles as shown by BrokenGlass which is more efficient for a large directory, but it's only available in .NET 4 and upwards.)

这篇关于C#类检查文件夹中是否存在文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆