如何遍历一个文件夹下的所有文件和子文件夹

yanlvbj 2009-10-19 10:13:36
编写一个函数,遍历一个文件夹下的所有文件和子文件夹
...全文
278 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
hejiasoft 2009-10-20
  • 打赏
  • 举报
回复
递归的方法,用FOR EACH()或者你写一个TREEVIEW的程序出来,你倒过来就明白了
flyerwing 2009-10-20
  • 打赏
  • 举报
回复
Directory.GetDirectories()
getfiles()
这两个搞定。
Csdbfans 2009-10-20
  • 打赏
  • 举报
回复
用递归是正解,foreach也说到点子上了,具体的程序应该不是很难
Snoworld 2009-10-20
  • 打赏
  • 举报
回复
睡神在睡觉 2009-10-20
  • 打赏
  • 举报
回复
- -貌似来晚了,递归搞定,mark
ConanKid 2009-10-20
  • 打赏
  • 举报
回复
这个代码写的比较早,是VB.NET的.呵呵.我之前是学VB的.
ConanKid 2009-10-20
  • 打赏
  • 举报
回复
下面这个是我自己写的拷贝某文件夹下所有的文件,你把拷贝的代码去掉就成遍历了.

Public Sub CopyDerictory(ByVal DirectorySrc As DirectoryInfo, ByVal DirectoryDes As DirectoryInfo)
Dim strDirectoryDesPath As String = DirectoryDes.FullName & "\" & DirectorySrc.Name

If Not Directory.Exists(strDirectoryDesPath) Then
Directory.CreateDirectory(strDirectoryDesPath)
End If

Dim f, fs() As FileInfo

fs = DirectorySrc.GetFiles()

For Each f In fs
File.Copy(f.FullName, strDirectoryDesPath & "\" & f.Name, True)
Next

Dim DirSrc, Dirs() As DirectoryInfo


Dirs = DirectorySrc.GetDirectories()

'递归调用自身
For Each DirSrc In Dirs
Dim DirDes As New DirectoryInfo(strDirectoryDesPath)
CopyDerictory(DirSrc, DirDes)
Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CopyDerictory(New DirectoryInfo("C:\Documents and Settings\username\Favorites"), New DirectoryInfo("g:\temp"))
End Sub


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ConanKid/archive/2005/08/18/458180.aspx
oneatree 2009-10-20
  • 打赏
  • 举报
回复
mark
cdc1982 2009-10-20
  • 打赏
  • 举报
回复
private void ChangeAllFileName(string path)
{
ChangeChildFileName(path);
foreach (string str in Directory.GetDirectories(path))
{
ChangeAllFileName(str + "\\");
}
}
/// <summary>
/// 更改文件名,将前缀为pre的文件名更改为_s,如:prea.jpg更名为a_s.jpg
/// </summary>
/// <param name="path"></param>
private void ChangeChildFileName(string path)
{

string fileName = "";
foreach (string str in Directory.GetFiles(path))
{
fileName = str.Substring(str.LastIndexOf('\\') + 1, str.Length - str.LastIndexOf('\\') - 1);
if (fileName.IndexOf("pre") == 0)
{
fileName = fileName.Replace("pre", "").Replace(".", "_s.");
if (File.Exists(path + fileName))
{
File.Delete(path + fileName);
}
File.Move(str, path + fileName);
}
}
}
feiren1421 2009-10-20
  • 打赏
  • 举报
回复
递归咯
无语中V5 2009-10-20
  • 打赏
  • 举报
回复
递归,foreach来显示了
lerit 2009-10-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhujiazhi 的回复:]
private void SearchFile(string path)
{
foreach(string dirPath in Directory.GetDirectories(path))
        {
SearchFile(dirPath);
        }
DirectoryInfo dirinfo = new DirectoryInfo(path);
foreach (FileInfo fileinfo in dirinfo.GetFiles())
        {
//你要做的事情
        }
}
[/Quote]
正解
zhujiazhi 2009-10-19
  • 打赏
  • 举报
回复
private void SearchFile(string path)
{
foreach(string dirPath in Directory.GetDirectories(path))
{
SearchFile(dirPath);
}
DirectoryInfo dirinfo = new DirectoryInfo(path);
foreach (FileInfo fileinfo in dirinfo.GetFiles())
{
//你要做的事情
}
}
perisonchen 2009-10-19
  • 打赏
  • 举报
回复
递归
lnwuyaowei 2009-10-19
  • 打赏
  • 举报
回复
应当是两个方法.
用递归.

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧