http://community.csdn.net/Expert/topic/3274/3274609.xml?temp=.6415979

解决方案 »

  1.   

    yes, use recursion or use SHFileOperation API, seehttp://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=lRrChWjgCHA.2308%40cpmsftngxa09http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=b8gimn%24upp%2402%241%40news.t-online.com
      

  2.   

    可以用DirectoryInfo的MoveTo,但这不算Copy吧
      

  3.   


    Q : 請問如何copy整個文件夾 
    主要解答者: chinchy 提交人: windsoft 
    感谢: chinchy、CoolMoon1001 
    审核者: TheAres 论坛对应贴子: 查看 
         A :  c#有沒有現成的方法調用  
    ---------------------------------------------------------------  
     
    没有现成的.  
    =========================  
    using  System;  
    using  System.IO;  
     
    namespace  FrameworkExamples  
    {  
           //HOW  TO:  recursively  copy  all  the  files  and  sub  dirs  in  a  given  directory  
           //                to  another  directory  
     
           class  SampleRecursiveCopy  
           {  
                   static  void  Main()  
                   {  
                           string  srcdir,  destdir;  
                           bool      recursive;  
     
                           recursive  =  true;  
                           srcdir  =  Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),  "images");  
                           destdir  =  Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),  "images2");  
     
                           FileCopy(srcdir,  destdir,  recursive);  
                   }  
     
                   private  static  void  FileCopy(string  srcdir,  string  destdir,  bool  recursive)  
                   {  
                           DirectoryInfo      dir;  
                           FileInfo[]            files;  
                           DirectoryInfo[]  dirs;  
                           string                    tmppath;  
     
                           //determine  if  the  destination  directory  exists,  if  not  create  it  
                           if  (!  Directory.Exists(destdir))  
                           {  
                                   Directory.CreateDirectory(destdir);  
                           }  
     
                           dir  =  new  DirectoryInfo(srcdir);  
                             
                           //if  the  source  dir  doesn't  exist,  throw  
                           if  (!  dir.Exists)  
                           {  
                                   throw  new  ArgumentException("source  dir  doesn't  exist  ->  "  +  srcdir);  
                           }  
     
                           //get  all  files  in  the  current  dir  
                           files  =  dir.GetFiles();  
     
                           //loop  through  each  file  
                           foreach(FileInfo  file  in  files)  
                           {  
                                   //create  the  path  to  where  this  file  should  be  in  destdir  
                                   tmppath=Path.Combine(destdir,  file.Name);                                  
     
                                   //copy  file  to  dest  dir  
                                   file.CopyTo(tmppath,  false);  
                           }  
     
                           //cleanup  
                           files  =  null;  
                             
                           //if  not  recursive,  all  work  is  done  
                           if  (!  recursive)  
                           {  
                                   return;  
                           }  
     
                           //otherwise,  get  dirs  
                           dirs  =  dir.GetDirectories();  
     
                           //loop  through  each  sub  directory  in  the  current  dir  
                           foreach(DirectoryInfo  subdir  in  dirs)  
                           {  
                                   //create  the  path  to  the  directory  in  destdir  
                                   tmppath  =  Path.Combine(destdir,  subdir.Name);  
     
                                   //recursively  call  this  function  over  and  over  again  
                                   //with  each  new  dir.  
                                   FileCopy(subdir.FullName,  tmppath,  recursive);  
                           }  
                             
                           //cleanup  
                           dirs  =  null;  
                             
                           dir  =  null;  
                   }  
           }  
    }  
     
     
     
    ---------------------------------------------------------------  
     
    方法一:使用API函数  
    public  class  ShellFiles  
     
    {  
     
    [StructLayout(LayoutKind.Sequential,  CharSet=CharSet.Auto,  Pack=1)]  
     
    public  struct  SHFILEOPSTRUCT  
     
    {  
     
    public  IntPtr  hwnd;  
     
    [MarshalAs(UnmanagedType.U4)]  public  int  wFunc;  
     
    public  string  pFrom;  
     
    public  string  pTo;  
     
    public  short  fFlags;  
     
    [MarshalAs(UnmanagedType.Bool)]  public  bool  fAnyOperationsAborted;  
     
    public  IntPtr  hNameMappings;  
     
    public  string  lpszProgressTitle;  
     
    }  
     
     
     
    [DllImport("shell32.dll",  CharSet=CharSet.Auto)]  
     
    static  extern  int  SHFileOperation(ref  SHFILEOPSTRUCT  FileOp);  
     
    const  int  FO_DELETE  =  3;  
     
    const  int  FO_COPY  =  2;  
     
    const  int  FOF_ALLOWUNDO  =  0x40;  
     
    const  int  FOF_NOCONFIRMATION  =  0x10;  //Don't  prompt  the  user.;  
     
    const  int  FOF_SIMPLEPROGRESS  =  0x100;  
     
    public  void  SendToRecyclyBin(string  path)  
     
    {  
     
    SHFILEOPSTRUCT  shf  =  new  SHFILEOPSTRUCT();  
     
    shf.wFunc  =  FO_DELETE;  
     
    shf.fFlags  =  FOF_ALLOWUNDO    ¦  FOF_NOCONFIRMATION;  
     
    shf.pFrom  =  path;  
     
    SHFileOperation(ref  shf);  
     
    }  
     
    public  void  Copy(string  from,  string  to)  
     
    {  
     
    DirectoryInfo  source  =  new  DirectoryInfo(from);  
     
    DirectoryInfo  dest  =  new  DirectoryInfo(to);  
     
    if(!dest.Exists)  
     
    dest.Create();  
     
    SHFILEOPSTRUCT  shf  =  new  SHFILEOPSTRUCT();  
     
    shf.wFunc  =  FO_COPY;  
     
    shf.fFlags  =  FOF_ALLOWUNDO;  
     
    shf.pFrom  =  from;  
     
    shf.pTo  =  to;  
     
    SHFileOperation(ref  shf);  
     
    }    
    }  
     
     
    方法二:递归调用(程序简单,但比较慢)  
    public  sealed  class  DirectoryUtils  
    {  
               private  DirectoryUtils()  
               {  
               }    
               ///  <summary>  
               ///        Copies  a  directory  to  a  new  location.  
               ///  </summary>  
               ///  <param  name="src">Source  directory  path</param>  
               ///  <param  name="dest">Destination  directory  path</param>  
     
               public  static  void  CopyDirectory(String  src,  String  dest)  
               {  
                           DirectoryInfo  di  =  new  DirectoryInfo(src);  
     
                           foreach(FileSystemInfo  fsi  in  di.GetFileSystemInfos())    
                           {  
                                       String  destName  =  Path.Combine(dest,  fsi.Name);  
                                       if  (fsi  is  FileInfo)  
                                                   File.Copy(fsi.FullName,  destName);  
                                       else    
                                       {  
                                                   Directory.CreateDirectory(destName);  
                                                   CopyDirectory(fsi.FullName,  destName);  
                                       }  
                           }  
               }  
    }  
     
     
      

  4.   

    using System;
    using System.IO;namespace Utility.IO{
        /// <summary>
        /// Filesystem
        /// </summary>
        public class FileSystem{
        
         public static void Main()
         {
         copyDirectory("source path","dst path");
         }
            // Copy directory structure recursively
            public static void copyDirectory(string Src,string Dst){
                String[] Files;            if(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar) 
                    Dst+=Path.DirectorySeparatorChar;
                if(!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
                Files=Directory.GetFileSystemEntries(Src);
                foreach(string Element in Files){
                    // Sub directories
                    if(Directory.Exists(Element)) 
                        copyDirectory(Element,Dst+Path.GetFileName(Element));
                    // Files in directory
                    else 
                        File.Copy(Element,Dst+Path.GetFileName(Element),true);
                    }
                }        }
        }