Search code examples
c#visual-studio-2010filefileinfo

c# - Filtering .zip file out of FileInfo array


I have a FileInfo array, which returns me 15 files and one ".zip" folder. I basically need to try and somehow filter this ".zip" file out of the array. Any help would be much appreciated, but it seems I am just banging my head against a brick wall! Here is the code that I am trying to impliment this into;

public List<FileInfo> getInfo(bool recursive, int ageDays)
{
  //Declarations
  DirectoryInfo di = new DirectoryInfo(CurrentFilePath);
  FileInfo[] fi = new FileInfo[0];
  List<FileInfo> results = new List<FileInfo>();
  fileResults = new List<Files>();
  DateTime ageInDays = DateTime.Now.AddDays(-ageDays);

  //Checks for recursive search
  if (recursive)
  {
    try
    {
      fi = di.GetFiles("*.*", SearchOption.AllDirectories);
    }
    catch (Exception)
    {
    }
  }
  else
  {
    try
    {
      fi = di.GetFiles();
    }
    catch (Exception)
    {
    }
  }

  for (int i = 0; i < fi.Length; i++)
  {
    if (fi[i].LastWriteTime < ageInDays)
    {
      results.Add(fi[i]);
    }
  }
  return results;
}

Thanks in advance!


Solution

  • Have you tried:

    for (int i = 0; i < fi.Length; i++)
            {
                if (fi[i].LastWriteTime < ageInDays && fi[i].Extension != ".zip")
                {
                    results.Add(fi[i]);
                }
            }