Share via


GetFiles(); all picture files

Question

Thursday, January 6, 2011 4:18 PM

ok  i am doing

f1 = d1.GetFiles("*.jpg");

but how can i work it where GetFiles will get all kinds of pictures file and not just  .jpg ?

but i only want picture files, and i need them to all be in f1

 

All replies (9)

Thursday, January 6, 2011 4:31 PM âś…Answered

Hi,

assuming d1 is a DirectoryInfo and f1 a FileInfo[] you can call the GetFiles-Method for each extension and add the newly found FileInfos to the Array.

      DirectoryInfo d1 = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));

      FileInfo[] f1 = d1.GetFiles("*.jpg");
      MessageBox.Show(f1.Length.ToString());
      f1 = f1.Concat(d1.GetFiles("*.png")).ToArray();
      MessageBox.Show(f1.Length.ToString());

Regards,

  Thorsten


Thursday, January 6, 2011 4:27 PM

Hi Riten900,

Can you explain a bit? Give some context, so that we know, for example, what f1 and d1 is and what you're trying to reach.

NVDPassie for CS

 


Thursday, January 6, 2011 4:30 PM

something like this,

 

 

string location = Application.StartupPath + "\\Pictures";
List<Image> images = new List<Image>();
string[] fileList = Directory.GetFiles(location)
string errorMessage = string.empty;

foreach (string file in fileList)
{
 try
 {
 images.Add(Image.FromFile(file));
 }
 catch(Exception)
 {
 errorMessage += "Wrong image file. File should not be in this directory!\n\n";
 }
}

MessageBox.Show(errorMessage);

 


Thursday, January 6, 2011 4:32 PM

Here is how I would go about something like that:

 

String WorkingDirectory = @"C:\MyDirectory"
DirectoryInfo diWorking = new DirectoryInfo(WorkingDirectory);
ArrayList alMyPictureFiles = new ArrayList();
Regex rgxPictureFiles = new Regex(@"bmp|png|jpg|gif"); // Add whatever picture extension items to this list you like

if (diWorking.Exists)
{
  foreach (FileInfo objFile in diWorking.GetFiles())
  {
    if (rgx.Match(objFile.Extension).Success)
    {
      alMyPictureFiles.Append(objFile);
    }
  }
}

Is this what you were looking for?


Thursday, January 6, 2011 4:33 PM

Hi,

it is always great to have just the required code, but you should provide all that is required. So it would be great to see, what dl is and fl.

In general: When you can get a list of all *.jpg files and all *.bmp files, you can merge the lists together.

So Directory.GetFiles(....) gives you an array of strings.

And with Array.Copy you can copy contents of one array to another array.

With kind regards,

Konrad


Thursday, January 6, 2011 4:41 PM

   DirectoryInfo di = new DirectoryInfo("C:\\Temp");
   String[] exts = { "*.png", "*.bmp" };
   ArrayList files = new ArrayList();
   foreach (string ext in exts)
   {
    files.AddRange(di.GetFiles(ext));
   }
   FileInfo[] fi = (FileInfo[])files.ToArray(typeof(FileInfo));

You can add more extensions to the exts array for the types you want to get.


Thursday, January 6, 2011 4:43 PM

I am doing it this way (optional enabling a recursive search through the directory tree)

Chris

string[] imageFiles = SearchByExtensions(@"C:\Images", false, "jpg", "jpeg","png", "bmp", "tiff", "tif", "gif");

using System.Linq;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

public static string[] SearchByExtensions(string startDir, bool recursive, params string[] extensions)
{
  bool all = extensions == null || extensions.Length == 0;
  Regex regexExtensions = null;
  if (!all)
  {
    StringBuilder regexBuilder = new StringBuilder(@"^.+\.(");
    regexBuilder.Append(string.Join("|", extensions));
    regexBuilder.Append(")$");

    regexExtensions = new Regex(regexBuilder.ToString(), RegexOptions.IgnoreCase | RegexOptions.Compiled);
  }
  return Directory.GetFiles(startDir, "*.*",
     recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
     .Where(fi => all || regexExtensions.IsMatch(fi))
     .ToArray();
}

Thursday, January 6, 2011 4:47 PM

  DirectoryInfo di = new DirectoryInfo("C:\\Temp");

  String[] exts = { "*.png", "*.bmp" };

  ArrayList files = new ArrayList();

  foreach (string ext in exts)

  {

  files.AddRange(di.GetFiles(ext));

  }

  FileInfo[] fi = (FileInfo[])files.ToArray(typeof(FileInfo));

You can add more extensions to the exts array for the types you want to get.

Self proposing ... I don't like it. Maybe the OP wants to use a different solution. In this thread there are a lot of fully functional ways shown by the poster's-replies.

Regards,

  Thorsten


Thursday, January 6, 2011 5:17 PM

Yes I'm sorry I need to watch my accidental clicks better. But to pass the buck a bit this forum needs some undo features. But now were getting off topic. 

 

And I like all proposed answers I see here there is always multiple ways to solve an issue.

 

Thanks,

 Scott