如何检查是否特定的文件在目录中存在或任何子目录 [英] How to check if a specific file exists in directory or any of its subdirectories

查看:213
本文介绍了如何检查是否特定的文件在目录中存在或任何子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我如何检查,如果一个特定的文件目录或任何子目录的存在?

System.IO.File.Exists 的似乎只接受没有重载一个参数来搜索子目录。

我可以用LINQ做到这一点,的 System.IO.Directory.GetFiles 的使用的 SearchOption.AllDirectories 的过载,但似乎有点重手。

  VAR MYLIST =从f由于Directory.GetFiles(tempScanStorage,foo.txt的,SearchOption.AllDirectories)
             其中,System.IO.Path.GetFileName(F).ToUpper()。包含(富)
             选择F;的foreach(在MYLIST VAR X)
{
    returnVal = x.ToString();
}


解决方案

如果你正在寻找一个特定的文件名,使用 *。* 确实重手。试试这个:

  var文件= Directory.GetFiles(tempScanStorage,富,SearchOption.AllDirectories)
                    .FirstOrDefault();
如果(文件== NULL)
{
    //处理文件未找到
}
其他
{
    //文件变量有文件名的第一个*发生*
}

请注意,这不是很你目前的查询做什么 - 因为你当前的查询会发现xbary.txt如果你富只是。我不知道这是否是有意还是无意。

如果您想了解多个匹配,则不应使用 FirstOrDefault()当然。目前尚不清楚究竟你想要做什么,这使得它很难给出更具体的建议。

请注意,在.NET 4中还有还有<一个href=\"http://msdn.microsoft.com/en-us/library/system.io.directory.enumeratefiles.aspx\"><$c$c>Directory.EnumerateFiles这可能会或可能不会对你有更好的表现。我高度怀疑,它会有所作为,当你搜索一个特定的文件(而不是目录和子目录中的所有的文件),但它的价值至少寂寂。编辑:正如在评论中指出,它可以有所作为的,如果你没有权限看到一个目录所有文件。

In C#, how do I check if a specific file exists in a directory or any of its subdirectories?

System.IO.File.Exists only seems to accept a single parameter with no overloads to search subdirectories.

I can do it with LINQ and System.IO.Directory.GetFiles using the SearchOption.AllDirectories overload, but that seems a bit heavy handed.

var MyList = from f in Directory.GetFiles(tempScanStorage, "foo.txt", SearchOption.AllDirectories)
             where System.IO.Path.GetFileName(f).ToUpper().Contains(foo)
             select f;

foreach (var x in MyList)
{
    returnVal = x.ToString();
}  

解决方案

If you're looking for a single specific filename, using *.* is indeed heavy handed. Try this:

var file = Directory.GetFiles(tempScanStorage, foo, SearchOption.AllDirectories)
                    .FirstOrDefault();
if (file == null)
{
    // Handle the file not being found
}
else
{
    // The file variable has the *first* occurrence of that filename
}

Note that this isn't quite what your current query does - because your current query would find "xbary.txt" if you foo was just bar. I don't know whether that's intentional or not.

If you want to know about multiple matches, you shouldn't use FirstOrDefault() of course. It's not clear exactly what you're trying to do, which makes it hard to give more concrete advice.

Note that in .NET 4 there's also Directory.EnumerateFiles which may or may not perform better for you. I highly doubt that it'll make a difference when you're searching for a specific file (instead of all files in the directory and subdirectories) but it's worth at least knowing about. EDIT: As noted in comments, it can make a difference if you don't have permission to see all the files in a directory.

这篇关于如何检查是否特定的文件在目录中存在或任何子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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