c#遍历一个文件夹下的所有文件包括子文件夹

本文介绍了一个使用 C# 实现的目录遍历示例,通过递归方式列出指定目录及其所有子目录下的文件,并显示文件的完整路径及大小。此代码适用于需要批量处理文件或了解文件系统的开发者。

using System;
using System.IO;

class ListAllFilesDemo
{
public static void Main()
{
Console.Write("请输入要查询的目录: ");
string dir = Console.ReadLine();
try
{
ListFiles(new DirectoryInfo(dir));
}
catch(IOException e)
{
Console.WriteLine(e.Message);
}
}

public static void ListFiles(FileSystemInfo info)
{
if(!info.Exists) return;

DirectoryInfo dir = info as DirectoryInfo;
//不是目录
if(dir == null) return;

FileSystemInfo [] files = dir.GetFileSystemInfos();
for(int i = 0; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
//是文件
if(file != null)
Console.WriteLine(file.FullName + "/t" + file.Length);
//对于子目录,进行递归调用
else
ListFiles(files[i]);

}
}
}

C#中,可以通过以下几种方式遍历文件夹里的文件。 ### 使用`DirectoryInfo`类 以下是一个使用`DirectoryInfo`类遍历指定文件夹及其文件夹中所有文件的示例代码: ```csharp using System; using System.IO; class Program { static void Main() { string path = @"E:/Tools"; // 替换为实际的文件夹路径 DirectoryInfo folder = new DirectoryInfo(path); TraverseFiles(folder); } static void TraverseFiles(DirectoryInfo folder) { try { // 获取当前文件夹中的所有文件 FileInfo[] files = folder.GetFiles(); foreach (FileInfo file in files) { Console.WriteLine(file.FullName); } // 递归遍历文件夹 DirectoryInfo[] subFolders = folder.GetDirectories(); foreach (DirectoryInfo subFolder in subFolders) { TraverseFiles(subFolder); } } catch (UnauthorizedAccessException) { // 处理没有权限访问的情况 Console.WriteLine($"没有权限访问文件夹: {folder.FullName}"); } catch (Exception ex) { Console.WriteLine($"发生错误: {ex.Message}"); } } } ``` 此方法利用`DirectoryInfo`类的`GetFiles`和`GetDirectories`方法,先获取当前文件夹中的所有文件并输出其完整路径,然后递归地遍历文件夹,从而实现对整个文件夹结构的遍历。 ### 构造文件树的方式遍历 若需要以文件树的形式展示文件夹结构,可以参考以下代码: ```csharp using System; using System.IO; using System.Windows.Forms; class Form1 : Form { private TreeView treeView1; public Form1() { treeView1 = new TreeView(); treeView1.Dock = DockStyle.Fill; this.Controls.Add(treeView1); this.Load += Form1_Load; } private void Form1_Load(object sender, EventArgs e) { TreeNode root = new TreeNode(); root.Text = "目录"; GetFiles(@"E:/Tools", root); treeView1.Nodes.Add(root); } private void GetFiles(string filePath, TreeNode node) { DirectoryInfo folder = new DirectoryInfo(filePath); node.Text = folder.Name; node.Tag = folder.FullName; FileInfo[] chldFiles = folder.GetFiles("*.*"); foreach (FileInfo chlFile in chldFiles) { TreeNode chldNode = new TreeNode(); chldNode.Text = chlFile.Name; chldNode.Tag = chlFile.FullName; node.Nodes.Add(chldNode); } DirectoryInfo[] chldFolders = folder.GetDirectories(); foreach (DirectoryInfo chldFolder in chldFolders) { TreeNode chldNode = new TreeNode(); chldNode.Text = chldFolder.Name; chldNode.Tag = chldFolder.FullName; node.Nodes.Add(chldNode); GetFiles(chldFolder.FullName, chldNode); } } } ``` 这种方式会将文件夹文件以树状结构展示在`TreeView`控件中,通过递归调用`GetFiles`方法,将每个文件夹及其文件夹文件添加到树节点中。 ### 使用关键词获取文件夹全称并遍历 如果不知道文件夹的准确名称,可使用关键词获取文件夹的全称,并返回全路径,然后再进行文件遍历,示例代码如下: ```csharp using System; using System.IO; class Program { static void Main() { string path = @"E:/"; // 替换为实际的根文件夹路径 string key = "Tools"; // 替换为实际的关键词 string folderName = GetFolderName(path, key); if (!string.IsNullOrEmpty(folderName)) { string fullPath = Path.Combine(path, folderName); DirectoryInfo folder = new DirectoryInfo(fullPath); TraverseFiles(folder); } } static string GetFolderName(string path, string key) { DirectoryInfo theFolder = new DirectoryInfo(path); // 遍历文件夹 foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) { if (NextFolder.Name.ToUpper().IndexOf(key.ToUpper()) >= 0) { return NextFolder.Name; } } return ""; } static void TraverseFiles(DirectoryInfo folder) { try { // 获取当前文件夹中的所有文件 FileInfo[] files = folder.GetFiles(); foreach (FileInfo file in files) { Console.WriteLine(file.FullName); } // 递归遍历文件夹 DirectoryInfo[] subFolders = folder.GetDirectories(); foreach (DirectoryInfo subFolder in subFolders) { TraverseFiles(subFolder); } } catch (UnauthorizedAccessException) { // 处理没有权限访问的情况 Console.WriteLine($"没有权限访问文件夹: {folder.FullName}"); } catch (Exception ex) { Console.WriteLine($"发生错误: {ex.Message}"); } } } ``` 此方法先通过`GetFolderName`方法根据关键词查找文件夹,然后对找到的文件夹进行文件遍历
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值