C#读取文件夹特定文件的方法

public image[] getImages()
{
    FolderBrowserDialog fbd = new FolderBrowserDialog();
    if (fbd.ShowDialog() == DialogResult.OK)
    {
      try
      {
        ///根据路径实例化一个对象
        var dirInfo = new     System.IO.DirectoryInfo(fbd.SelectedPath);
        ///选出所有符合一定后缀的文件列表,此处选择的是图像文件
        mySelectedImages = dirInfo.GetFiles("*.*", System.IO.SearchOption.AllDirectories)
          .Where(info => IsRight(info)).ToArray();
      }
      catch (Exception ex)
      {
        LogHelper.LogError(ex);
      }
    }
}

private bool IsRight(System.IO.FileInfo info)
{
    //选择的文件后缀名
    List<string> patterns = new List<string>() { ".png", ".jpg", ".bmp", ".tif" };
    return patterns.Contains(info.Extension);
}

 

posted @ 2016-10-13 10:20  Ziev  阅读(486)  评论(0编辑  收藏  举报