在ASP.NET中用C#开发,上传整个文件夹(里面包含多层子文件夹),上传后保持文件夹结构不变。我想用树的遍历的方式来做,但对于C#中如何遍历、遍历时用到的参数和方法,以及如何取得树结构还不太了解。另外很可能还有更好的办法,还望高手指点!先谢谢了!

解决方案 »

  1.   

    找子目录可以这样做: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());
            }
        }
    }
      

  2.   

    某个目录下的所有文件:
    using System;
    using System.IO;class Test 
    {
        public static void Main() 
        {
            try 
            {
                // Only get files that begin with the letter "c."
                string[] dirs = Directory.GetFiles(@"c:\", "c*");
                Console.WriteLine("The number of files starting with c is {0}.", dirs.Length);
                foreach (string dir in dirs) 
                {
                    Console.WriteLine(dir);
                }
            } 
            catch (Exception e) 
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            }
        }
    }
      

  3.   

    //取本地目录内的文件名
    private void showDirFiles(string strPath)
    {
    try
    {
    if (!Directory.Exists(strPath))
    {//目录不存在,创建目录
    Directory.CreateDirectory(strPath);
    } DirectoryInfo mydir=new DirectoryInfo(strPath);
    FileInfo [] files=mydir.GetFiles();
    this.listBoxC.Items.Clear();//清楚listbox中现有项目
    for(int i=0;i<files.Length;i++)
    {
    this.listBoxC.Items.Add(files[i].ToString());
    }
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }
      

  4.   

    递归实现查找目录下的所有子目录和文件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;
      

  5.   

    给个我在c++下文件查找备份文件的函数吧,参考一下:
    void  TfrmMain::FileSearch(AnsiString strOldPath,AnsiString strNewPath)
    {
      AnsiString strTemp,tmpName,OldPath,NewPath,tmpPath1,tmpPath2;
      TDateTime datetime,dtOld,dtNew,dtNew1;
      TSearchRec FileOld,FileNew;
      int fdFind;
      int fdFindNew;  OldPath=strOldPath;
      NewPath=strNewPath;
      OldPath=OldPath+"\\";
      NewPath=NewPath+"\\";
      /////////////////////////列举所有目录,不存在就创建///////////////////////////
      if(FindFirst(OldPath+"*.*",faAnyFile,FileOld)==0)
      {
       do
          {
             //判断是否是目录,并排除目录“.”和“..”
             if((FileOld.Attr & faDirectory) && FileOld.Name!="." && FileOld.Name!="..")
             {
               
               tmpPath1=OldPath+FileOld.Name;
               tmpPath2=NewPath+FileOld.Name;
               if(!DirectoryExists(tmpPath2))
                    CreateDir(tmpPath2);
               //调用函数本身,进入子目录
               FileSearch(tmpPath1,tmpPath2);
              }      } while (FindNext(FileOld) == 0);
         FindClose(FileOld);
      }
      //////////////////////////////////列举所有文件,比较后进行拷贝////////////////
      if(FindFirst(OldPath+"*.*",faAnyFile,FileOld)==0)
      {
             do
           {          if(!(FileOld.Attr & faDirectory))           {
                  strTemp=OldPath+FileOld.Name;
                  tmpName=NewPath+FileOld.Name;
                  fdFind=FindFirst(tmpName,faAnyFile,FileNew);
                    if(fdFind==0)
                    {
                            AnsiString anotherOldName,anotherNewName;
                            anotherOldName=OldPath+"tmp"+FileOld.Name;
                            anotherNewName=NewPath+"tmp"+FileOld.Name;
                            fdFindNew=FindFirst(anotherNewName,faAnyFile,FileNew);
                            if(fdFindNew==0)
                            {
                                    dtOld=FileDateToDateTime(FileAge(strTemp.c_str()));
                                    dtNew=FileDateToDateTime(FileAge(tmpName.c_str()));
                                    dtNew1=FileDateToDateTime(FileAge(anotherNewName.c_str()));
                                    if(dtNew>dtNew1)
                                    {
                                         if(dtOld>dtNew1)
                                         {
                                            CopyFile(strTemp.c_str(),anotherNewName.c_str(),FALSE);
                                            iNum++;
                                            txt_info->Lines->Add(IntToStr(iNum)+") "+strTemp+"    "+FormatDateTime("yyyy-mm-dd  hh:mm:ss",datetime.CurrentDateTime()));
                                         }
                                    }
                                    else
                                    {
                                        if(dtOld>dtNew)
                                          {
                                            CopyFile(strTemp.c_str(),tmpName.c_str(),FALSE);
                                            iNum++;
                                            txt_info->Lines->Add(IntToStr(iNum)+") "+strTemp+"    "+FormatDateTime("yyyy-mm-dd  hh:mm:ss",datetime.CurrentDateTime()));
                                          }
                                    }
                                    /*
                                    CopyFile(strTemp.c_str(),tmpName.c_str(),FALSE);
                                    iNum++;
                                    txt_info->Lines->Add(IntToStr(iNum)+") "+strTemp+"    "+FormatDateTime("yyyy-mm-dd  hh:mm:ss",datetime.CurrentDateTime()));
                                    */
                            }
                            else
                            {
                                    CopyFile(strTemp.c_str(),anotherNewName.c_str(),FALSE);
                                    iNum++;
                                    txt_info->Lines->Add(IntToStr(iNum)+") "+strTemp+"    "+FormatDateTime("yyyy-mm-dd  hh:mm:ss",datetime.CurrentDateTime()));
                            }
                            /*
                            dtOld=FileDateToDateTime(FileAge(strTemp.c_str()));
                            dtNew=FileDateToDateTime(FileAge(tmpName.c_str()));
                            if(dtOld>dtNew)
                            {
                                    CopyFile(strTemp.c_str(),tmpName.c_str(),FALSE);
                                    iNum++;
                                    txt_info->Lines->Add(IntToStr(iNum)+") "+strTemp+"    "+FormatDateTime("yyyy-mm-dd  hh:mm:ss",datetime.CurrentDateTime()));
                            }
                            */
                    }
                    else
                    {
                            CopyFile(strTemp.c_str(),tmpName.c_str(),TRUE);
                            iNum++;
                            txt_info->Lines->Add(IntToStr(iNum)+") "+strTemp+"    "+FormatDateTime("yyyy-mm-dd  hh:mm:ss",datetime.CurrentDateTime()));
                     }
                    FindClose(FileNew);
                }
            } while (FindNext(FileOld) == 0);
          FindClose(FileOld);
      }
    }
      

  6.   

    http://blog.csdn.net/zhzuo/archive/2005/03/08/315125.aspx