Home  >  Article  >  类库下载  >  C# method to read specific files in a folder

C# method to read specific files in a folder

高洛峰
高洛峰Original
2016-10-14 17:05:411630browse

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);
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn