• C# 获取某路径文件夹中全部图片或其它指定格式的文件名(全路径)


    1、编写获取文件名(全路径)子函数
    /////param
    ///path:文件夹路径
    ///suffix:后缀格式, 如bmp,txt
    ///fileList:文件名存放
    ///isSubcatalog:true遍历子文件夹,否则不遍历
    void
    getFiles(string path, string suffix, ref List<string> fileList, bool isSubcatalog) {   string filename;   DirectoryInfo dir = new DirectoryInfo(path);   FileInfo[] file = dir.GetFiles();   //DirectoryInfo[] dii = dir.GetDirectories();//如需遍历子文件夹时需要使用   foreach (FileInfo f in file)   {     filename = f.FullName;     if (filename.EndsWith(suffix))//判断文件后缀,并获取指定格式的文件全路径增添至fileList     {       fileList.Add(filename);     }   }   获取子文件夹内的文件列表,递归遍历
    if(isSubcatalog)
    {
       foreach (DirectoryInfo d in dii)    {    getFiles(d.FullName, fileList);    }
    }
      return; }

    2、在界面中放置一个button控件,单击按钮时弹出文件夹路径选择窗口,并调用getFiles子函数:

    List<string> imageFiles = new List<string>();
    private void btnSelectPath_Click(object sender, EventArgs e)
    {
      FolderBrowserDialog dialog = new FolderBrowserDialog();
      dialog.Description = "Please choose image path.";
      DialogResult result = dialog.ShowDialog();
      if (result == System.Windows.Forms.DialogResult.Cancel)
      {
        return;
      }
      string folderPath = dialog.SelectedPath.Trim();
      DirectoryInfo theFolder = new DirectoryInfo(folderPath);
      if (theFolder.Exists)
      {
        getFiles(folderPath,"bmp", ref imageFiles, false);
        return; 
      }
    }
    One day,I will say "I did it"
  • 相关阅读:
    解决JAVA调用C++ DLL文件Unable to load library的问题
    java调用C或者C++动态库dll
    C# 实现 ICMP PING 功能
    利用Ngnix代理基于Baget搭建私有Nuget服务
    自动驾驶常见术语的缩写和释义 微信公众号
    单细胞公共数据挖掘
    TCP标志信息
    [Javascript] Extending debounce with a maxWait Option
    [Javascript] Build lodash.merge from Scratch
    [Typescript] When to use generic type?
  • 原文地址:https://www.cnblogs.com/Vince-Wu/p/11751856.html
Copyright © 2020-2023  润新知