How to check the filenames in a folder

I would like to be able to iterate over the names 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 view names and be able to say

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

      

Something like that

thank

+1


source to share


6 answers


You must use a static method Exists

on System.IO.File

.

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

      

Because the method returns a Boolean value, there is no need for an expression in the example in the example if

.

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



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 will search for the specified folder and the entire subfolder with the "dog.jpg" template. The extension method Any()

just checks if it contains IEnumerable

all the elements. I think this is the most efficient way to do it (based on gut feeling).

+7


source


From http://weblogs.asp.net/israelio/archive/2004/06/23/162913.aspx Just paste 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);
  }

      



}

+1


source


get all files

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

      

and iterating through it

+1


source


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;

      

+1


source


use Directory.GetFiles ()

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

      

+1


source


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

      

+1


source







All Articles