开发者

How to check for file names in a folder

I would like to be able to iterate through the name of some image files in a folder using c#. So for intance I have a folder named image and contains the following images

image dog.jpg cat.jpg horse.jpg

I want to be able to go through the names and be able to say

if(filename == dog.jpg)
  return true
else
return false

Something of that 开发者_运维百科nature

Thank you


You should use the static Exists method on System.IO.File.

return System.IO.File.Exists("dog.jpg")

Since the method returns a boolean, there is no need for the if statement in the example you gave.

You can also use a bit of Linq magic to determine if a file exists in a folder structure, like this:

var dir = new System.IO.DirectoryInfo(startFolder);
var fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
bool fileExists = fileList.Any(f => f.FullName == "dog.jpg");

or even shorter:

return System.IO.Directory
   .GetFiles(@"c:\myfolder", "dog.jpg", SearchOption.AllDirectories)
   .Any();

which would search the folder specified and all subfolder with the pattern "dog.jpg". The Any() extension method simply checks whether the IEnumerable contains any items. I think this is the most efficient way of doing this (based on gut feeling).


From http://weblogs.asp.net/israelio/archive/2004/06/23/162913.aspx Just insert your if in the "// do something with filename" area:

// How much deep to scan. (of course you can also pass it to the method)
const int HowDeepToScan=4;

public static void ProcessDir(string sourceDir, int recursionLvl) 
{
  if (recursionLvl<=HowDeepToScan)
  {
    // Process the list of files found in the directory.
    string [] fileEntries = Directory.GetFiles(sourceDir);
    foreach(string fileName in fileEntries)
    {
       // do something with fileName
       Console.WriteLine(fileName);
    }

    // Recurse into subdirectories of this directory.
    string [] subdirEntries = Directory.GetDirectories(sourceDir);
    foreach(string subdir in subdirEntries)
       // Do not iterate through reparse points
       if ((File.GetAttributes(subdir) &
            FileAttributes.ReparsePoint) !=
                FileAttributes.ReparsePoint)

            ProcessDir(subdir,recursionLvl+1);
  }

}


get all the files

string[] filePaths = Directory.GetFiles(@"c:\yourfolder\");

and iterate through it


use Directory.GetFiles()

foreach(var file in (myDir.GetFiles("*.jpg")
{
   if(file.Name == "dog.jpg") return true;
}


        var files = System.IO.Directory.GetFiles("directory", "*.jpg");
        foreach (var item in files)
        {
            if (System.IO.Path.GetFileName(item) == "dog.jpg")
            { 
                // File found.                
            }
        }


DirectoryInfo di = new DirectoryInfo("c:\\Images");
 var files = di.GetFiles("*.jpg");
 foreach (var fileInfo in files)
 {
     if (fileInfo.Name == "dog.jpg")
              return true;
 }
 return false;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜