Check Directory by path and extension - CSharp File IO

CSharp examples for File IO:Directory

Description

Check Directory by path and extension

Demo Code

//     Copyright (c) Peony. All rights reserved.
using System.Linq;
using System.IO;/*from   w ww  .  j  av  a 2 s  .c om*/
using System.Collections.Generic;

public class Main{
        public static List<FileInfo> CheckDirectory(string path, string extension)
        {
            List<FileInfo> extensionPaths = new List<FileInfo>();
            try
            {
                if (!File.Exists(path))
                {
                    if (Directory.Exists(path))
                    {
                        DirectoryInfo dir = new DirectoryInfo(path);
                        FileInfo[] infos = dir.GetFiles();
                        extensionPaths.AddRange(infos.Where(info => Path.GetExtension(info.FullName) == extension));
                    }
                }
                else
                    return null;
            }
            catch (System.Exception)
            {
                return null;
            }

            return extensionPaths;
        }
}

Related Tutorials