Directory.GetFiles 方法
[C#]
public static string[] GetFiles(
   string path
);
参数
path 
将从其检索文件的目录。 
返回值
指定目录中文件名的 String 数组。备注
此方法与将星号 (*) 指定为搜索模式的 GetFiles(String, String) 方法相同。允许 path 参数指定相对或绝对路径信息。相对路径信息被解释为相对于当前工作目录。若要获取当前工作目录,请参见 GetCurrentDirectory。path 参数不区分大小写。

解决方案 »

  1.   

    .NET Framework 类库   获取指定目录中子目录的名称。重载列表
    获取指定目录中子目录的名称。受 .NET Framework 精简版的支持。[C#] public static string[] GetDirectories(string);
    从当前目录获取与指定搜索模式匹配的目录的数组。受 .NET Framework 精简版的支持。[C#] public static string[] GetDirectories(string, string);
    示例[C#] 
    using System;
    using System.IO;class Test 
    {
        public static void Main() 
        {
            try 
            {
                // Only get subdirectories that begin with the letter "p."
                string[] dirs = Directory.GetDirectories(@"c:\", "p*");
                Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length);
                foreach (string dir in dirs) 
                {
                    Console.WriteLine(dir);
                }
            } 
            catch (Exception e) 
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            }
        }
    }请参见
    Directory 类 | Directory 成员 | System.IO 命名空间 | C++ 托管扩展编程 
      

  2.   

    这只能列出某个目录下的子目录名和文件名
    我想实现的结果如下:
    c:\1.txt
    c:\2.txt
    c:\a\1.txt
    c:\f\2\f\3.txt
    就是把某个目录下的包括其子目录中的所有文件名列出
      

  3.   

    标题   递归算法--遍历指定目录下的子目录及文件(C#.net)     选择自 chhwang 的 Blog  
    关键字   递归算法--遍历指定目录下的子目录及文件(C#.net) 
    出处    
     
     //递归算法--遍历指定目录下的子目录及文件(C#.net),希望有用,顺带数据入库   private void button1_Click(object sender, System.EventArgs e) 
      { 
       Conn.Open();    displayItems(textBox1.Text); 
       //MessageBox.Show(dirs.Length.ToString()); 
       Conn.Close(); 
      } 
     /*这里是递归算法的遍历程序部分*/ 
      private void displayItems(string path) 
      { 
       try 
       { 
        DirectoryInfo di=new DirectoryInfo(path); 
        FileInfo[] SubFiles=di.GetFiles();     FileSystemInfo[] dirs = di.GetDirectories();       foreach(FileInfo fileNext in SubFiles) 
          { 
         /*-----------------------------------------------------------------------------------------------------*/ 
           string path_total=path + "/" +fileNext.ToString(); 
           int path_start=path_total.IndexOf("/")+1; 
           int path_end=path_total.LastIndexOf("/"); 
           string path_name; 
           if(path_start-1==path_end) 
           { 
             path_name=""; 
           } 
           else 
           { 
             path_name=path.Substring(path_start,path_end-path_start); 
           } 
           switch(path_start.ToString()) 
           { 
            case "....": 
             break; 
           } 
         /*-----------------------------------------------------------------------------------------------------*/ 
           string sql="insert into pic_data(pic_name,pic_path,pic_time) values(’"+ fileNext.ToString() +"’,’"+ path_name +"’,’"+ DateTime.Now.ToString() +"’)"; 
           SqlCommand Cmd=new SqlCommand(sql,Conn); 
           if(checkBox_insertdb.Checked==true) 
           Cmd.ExecuteNonQuery();        richTextBox1.Text=richTextBox1.Text + "\r\n" + path + "/" +fileNext.ToString() + "  ("+sql+")"; 
          } 
        foreach(DirectoryInfo diNext in dirs) 
        { 
         displayItems(path + "/" + diNext.ToString()); 
        } 
       } 
       catch(Exception ex) 
       { 
        richTextBox1.Text=ex.Message +"\r\n"+ richTextBox1.Text; 
       }   } 
     } 
     //供参考