c#中寻找文件夹根目录效果

来源:百度知道 编辑:UC知道 时间:2024/09/23 11:22:32
希望能有详细的代码
能不能再详细点

详细代码

递归实现查找目录下的所有子目录和文件
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]!='\\')