2个回答
2013-06-09
展开全部
递归//所有文件信息 string fileInfo = string.Empty; private void GetAllFiles() { if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { DirectoryInfo dInfo = new DirectoryInfo(folderBrowserDialog.SelectedPath); //遍历该文件夹 GetFolder(dInfo); } using (System.IO.StreamWriter sw = new StreamWriter (Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"\\fileInfo.txt",false, Encoding.GetEncoding("gb2312"))) { sw.Write(fileInfo); sw.Flush(); } } private void GetFolder(DirectoryInfo dInfo) { //显示其中文件 GetFile(dInfo); //遍历文件夹中的文件夹 foreach (DirectoryInfo dir in dInfo.GetDirectories()) { //递归遍历该文件夹 GetFolder(dir); } } private void GetFile(DirectoryInfo dInfo) { //遍历文件夹中的文件 foreach (FileInfo file in dInfo.GetFiles()) { if(file.Extension.Equal(".jpg")||file.Extension.Equal(".gif")||file.Extension.Equal(".bmp")) { //这里就获取到了 fileInfo+=file.Name+"\r\n"; } Application.DoEvents(); } }
2013-06-09
展开全部
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading; namespace copefile
{
class Program
{
static void Main(string[] args)
{
string testDir = "e:/xunlei/";
listFiles(testDir,0);
Console.ReadKey();
} public static void listFiles(string dir, int level)
{
try
{
//获取文件列表
string[] files = Directory.GetFiles(dir); String preStr = "";
for (int i = 0; i < level; i++)
{
preStr += " ";
} foreach (string f in files)
{
if (f.LastIndexOf("\\") == -1)
{
Console.WriteLine(preStr + f.Substring(f.LastIndexOf("/") + 1));
}
else
{
Console.WriteLine(preStr + f.Substring(f.LastIndexOf("\\") + 1));
} } //获取目录列表
string[] dirs = Directory.GetDirectories(dir);
foreach (string d in dirs)
{
if (d.LastIndexOf("\\") == -1)
{
Console.WriteLine(preStr + d.Substring(d.LastIndexOf("/") + 1));
}
else
{
Console.WriteLine(preStr + d.Substring(d.LastIndexOf("\\") + 1));
}
if (Directory.Exists(d))
{
listFiles(d, level + 1);
}
} }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
} 一上是遍历磁盘的程序。可以参考一下哦!
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading; namespace copefile
{
class Program
{
static void Main(string[] args)
{
string testDir = "e:/xunlei/";
listFiles(testDir,0);
Console.ReadKey();
} public static void listFiles(string dir, int level)
{
try
{
//获取文件列表
string[] files = Directory.GetFiles(dir); String preStr = "";
for (int i = 0; i < level; i++)
{
preStr += " ";
} foreach (string f in files)
{
if (f.LastIndexOf("\\") == -1)
{
Console.WriteLine(preStr + f.Substring(f.LastIndexOf("/") + 1));
}
else
{
Console.WriteLine(preStr + f.Substring(f.LastIndexOf("\\") + 1));
} } //获取目录列表
string[] dirs = Directory.GetDirectories(dir);
foreach (string d in dirs)
{
if (d.LastIndexOf("\\") == -1)
{
Console.WriteLine(preStr + d.Substring(d.LastIndexOf("/") + 1));
}
else
{
Console.WriteLine(preStr + d.Substring(d.LastIndexOf("\\") + 1));
}
if (Directory.Exists(d))
{
listFiles(d, level + 1);
}
} }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
} 一上是遍历磁盘的程序。可以参考一下哦!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询