不取得子目录的话用这个: 
using  System.IO; 
string[]  dirs  =  Directory.GetDirectories(@"c:\");//路径 
foreach  (string  dir  in  dirs)   

     Console.WriteLine(dir); 

文件的话把GetDirectories改成GetFiles 
 
如果要递归的话,就是想取得此目录下所有子目录和文件的办法用这个: 
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;

相关文章: