查找指定目录下的文件夹或文件
2014-05-20 09:47
423 查看
转载:zhidao.baidu.com/link?url=g19YhEZMppu4ERsC0Ai54i2maTyIdErAIKIxUPnwzr1U_AfapEudsJ_J9Iza4fforNxjap-f2_DRi_nmydpie_
//查找指定路径文件是否存在
File.Exists("路径");
//递归实现查找目录下的所有子目录和文件;
public void FindFile(string dir) //参数为指定的目录
{
//在指定目录及子目录下查找文件,在listBox1中列出子目录及文件
DirectoryInfo Dir = new DirectoryInfo(dir);
try
{
foreach (DirectoryInfo d in Dir.GetDirectories()) //查找子目录
{
FindFile(Dir + d.ToString() + "\\");
listBox1.Items.Add(Dir + d.ToString() + "\\"); //listBox1中填加目录名
}
foreach (FileInfo f in Dir.GetFiles("*.*")) //查找文件
{
listBox1.Items.Add(Dir + f.ToString()); //listBox1中填加文件名
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
//调用
private void button1_Click(object sender, System.EventArgs e)
{
string currentdir="F:\\myprogram\\C#\\FileSearch"; //搜索的目录
if(currentdir[currentdir.Length-1]!='\\') //非根目录
currentdir+="\\";
FindFile(currentdir); //调用查找文件函数
}
//引用
using System.IO;
//查找指定路径文件是否存在
File.Exists("路径");
//递归实现查找目录下的所有子目录和文件;
public void FindFile(string dir) //参数为指定的目录
{
//在指定目录及子目录下查找文件,在listBox1中列出子目录及文件
DirectoryInfo Dir = new DirectoryInfo(dir);
try
{
foreach (DirectoryInfo d in Dir.GetDirectories()) //查找子目录
{
FindFile(Dir + d.ToString() + "\\");
listBox1.Items.Add(Dir + d.ToString() + "\\"); //listBox1中填加目录名
}
foreach (FileInfo f in Dir.GetFiles("*.*")) //查找文件
{
listBox1.Items.Add(Dir + f.ToString()); //listBox1中填加文件名
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
//调用
private void button1_Click(object sender, System.EventArgs e)
{
string currentdir="F:\\myprogram\\C#\\FileSearch"; //搜索的目录
if(currentdir[currentdir.Length-1]!='\\') //非根目录
currentdir+="\\";
FindFile(currentdir); //调用查找文件函数
}
//引用
using System.IO;
相关文章推荐
- 根据提供的文件名文本,查找指定目录下的该文件,找到后复制到指定文件夹--bash
- java 遍历指定目录下的文件夹并查找包含指定关键字的文件
- 查找程序所在上一级目录中是否存在指定文件夹,不存在就建立,并在其中新建一个txt文件
- 用python查找在指定目录下特定文件夹下的指定文件
- 从目录中查找指定后缀文件内的指定字符串
- vb查找指定文件夹下面的所有文件和其子目录下的文件
- 删除指定时间前目录下生成的所有文件、文件夹
- asp.net 删除文件夹,指定文件夹,删除文件夹和所有文件,删除权限设置,递归删除文件夹目录及文件
- 查找指定文件夹下最后修改的文件
- 保存指定文件夹下的目录和文件名到文件中 Python 脚本
- asp.net 删除文件夹,指定文件夹,删除文件夹和所有文件,删除权限设置,递归删除文件夹目录及文件
- 判断所指定的文件或目录(文件夹)是否存在存储过程
- C#查找指定文件夹下指定后缀名的所有文件
- vc__在指定目录下查找文件
- 批处理复制最新的文件夹里面的bmp文件到指定目录
- 将指定文件夹下的psd文件放到指定目录--内含文件批处理类
- VB.NET 在指定文件夹查找文件 类似的文件
- 查找指定目录下所有文件(包括子目录)——使用递归
- JAVA 的递归方法查找目录下的文件和文件夹
- 将指定目录的所有文件及文件夹copy到指定目录下,只copy 7天内创建的或是7天内修改过的