C#遍历文件夹获取指定后缀名文件
作者:cell006 时间:2023-01-07 02:07:56
本文实例为大家分享了C#遍历文件夹获取指定后缀名文件的具体代码,供大家参考,具体内容如下
问题描述:
项目需要,要进行某文件夹下所有shp数据的读取
解决方法:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace learnGDAL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
List<FileInfo> lst = new List<FileInfo>();
string strPath = @"E:\WORK\g1\北京市\北京市";
List<FileInfo> lstFiles = getFile(strPath, ".shp",lst);
foreach(FileInfo shpFile in lstFiles)
{
label3.Text += shpFile.FullName+"\n";
}
}
/// <summary>
/// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
/// </summary>
/// <param name="path">文件夹路径</param>
/// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
/// <returns>List<FileInfo></returns>
public static List<FileInfo> getFile(string path, string extName, List<FileInfo> lst)
{
try
{
string[] dir = Directory.GetDirectories(path); //文件夹列表
DirectoryInfo fdir = new DirectoryInfo(path);
FileInfo[] file = fdir.GetFiles();
//FileInfo[] file = Directory.GetFiles(path); //文件列表
if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
{
foreach (FileInfo f in file) //显示当前目录所有文件
{
if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
{
lst.Add(f);
}
}
foreach (string d in dir)
{
getFile(d, extName,lst);//递归
}
}
return lst;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
效果:
来源:https://blog.csdn.net/funkstill/article/details/88198461
标签:C#,遍历,文件夹
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
unity实现场景切换进度条显示
2022-05-16 20:56:42
![](https://img.aspxhome.com/file/2023/5/104305_0s.jpg)
分享Android仿刮奖效果控件
2021-12-15 16:00:16
![](https://img.aspxhome.com/file/2023/6/89556_0s.jpg)
C#中split用法实例总结
2023-11-27 11:31:16
Netty组件NioEventLoopGroup创建线程执行器源码解析
2022-03-07 00:36:17
![](https://img.aspxhome.com/file/2023/5/98115_0s.png)
Unity实现文本转贴图
2022-05-10 19:53:04
![](https://img.aspxhome.com/file/2023/7/100237_0s.jpg)
高并发下restTemplate的错误分析方式
2023-08-23 12:58:24
![](https://img.aspxhome.com/file/2023/4/84614_0s.png)
如何使用Spring AOP预处理Controller的参数
2022-09-11 10:01:56
![](https://img.aspxhome.com/file/2023/1/73121_0s.png)
Android利用手势完成屏幕密码锁功能
2023-04-08 21:21:40
![](https://img.aspxhome.com/file/2023/1/139011_0s.jpg)
Java中LinkedList和ArrayList的效率分析
2023-02-12 06:21:55
![](https://img.aspxhome.com/file/2023/9/61809_0s.png)
Springcloud seata分布式事务实现代码解析
2022-12-27 20:14:01
![](https://img.aspxhome.com/file/2023/0/66050_0s.png)
Android AMS启动详解
2023-08-26 16:13:52
Java关键字volatile详析
2023-01-21 21:51:11
![](https://img.aspxhome.com/file/2023/5/129185_0s.gif)
C语言回溯法解八皇后问题(八皇后算法)
2021-11-12 03:27:15
![](https://img.aspxhome.com/file/2023/0/120790_0s.jpg)
Spring Cloud Gateway(读取、修改 Request Body)的操作
2023-11-09 19:25:46
![](https://img.aspxhome.com/file/2023/0/59240_0s.jpg)
Mybatis-plus配置分页插件返回统一结果集
2022-05-27 19:15:06
![](https://img.aspxhome.com/file/2023/1/88061_0s.jpg)
Java框架MyBatis接口编程过程解析
2022-09-18 07:47:04
c#哈希算法的实现方法及思路
2023-10-22 01:48:08
![](https://img.aspxhome.com/file/2023/3/113803_0s.png)
基于spring cloud多个消费端重复定义feign client的问题
2022-09-18 11:34:23
详解spring boot 使用application.properties 进行外部配置
2023-02-13 02:09:11
![](https://img.aspxhome.com/file/2023/7/125227_0s.png)
C#使用Selenium的实现代码
2021-11-16 05:58:25
![](https://img.aspxhome.com/file/2023/9/129709_0s.png)