教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 c#文件夹 递归访问的实现代码

c#文件夹 递归访问的实现代码

发布时间:2015-11-17   编辑:jiaochengji.com
用c#递归访问多层文件夹中的内容,在有关c#文件夹的编程中经常碰到。为大家提供一段c#代码,实现删除当前文件夹及子目录中的音乐文件。

用c#递归访问多层文件夹中的内容,在有关c#文件夹的编程中经常碰到。
为大家提供一段c#代码,实现删除当前文件夹及子目录中的音乐文件。有需要的朋友,可以参考下。
 

复制代码 代码示例:
/// <summary>
/// c# 文件夹 递归访问
/// </summary>
using System;
using System.IO;
namespace DelAllLrcFiles
{
class Program
{
static readonly string root = @"F:\mymusic\files\";
static readonly string ext = ".lrc";
static void Main(string[] args)
{
DelAllLrc(root);
}
/// <summary>
/// 删除文件夹及子文件夹中的文件。
/// </summary>
static void DelAllLrc(string path)
{
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
if (file.EndsWith(ext, StringComparison.CurrentCultureIgnoreCase))
{
try
{
File.Delete(file);
}
catch
{
Console.WriteLine("出错了,文件:" + file);
}
}
}
string[] dirs = Directory.GetDirectories(path);
foreach (string dir in dirs)
{
DelAllLrc(dir);
}
}
}
}

您可能感兴趣的文章:
c#文件夹 递归访问的实现代码
一文了解Python中的递归
PHP递归算法实例解析
php递归创建目录小例子
php使用mkdir创建多级目录的方法
php无限遍历目录代码
PHP遍历文件和文件夹的小例子
php递归函数使用return问题
删除指定文件夹中所有文件的php代码
php 按修改日期保存文件到日期文件夹的实例代码

[关闭]
~ ~