同一台机器上的不同盘符下文件夹竞然不支持拷贝?
难道我非得写一递归去一个个拷??
真FT!1
有高人知道有什么法子简单点的,就像拷贝文件一样去拷贝文件夹吗?

解决方案 »

  1.   

    看了帖子随手写了个,写着好玩,呵呵
    static void XCopy( string FromFolderPath , string ToFolderPath )
            {
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
                string strCommand = "xcopy " + FromFolderPath + " " + ToFolderPath;
                p.StandardInput.WriteLine(strCommand);
                p.StandardInput.WriteLine("D");
                p.Close();
            }
      

  2.   

    楼上
    我传进去两个文件夹路径
    怎么不可以吗?
     XCopy( "e:\\abc" , "d:\\test" )
    abc为要拷贝的目标文件夹,test为目的地文件夹
    代码没出错跑过去

    但没有效果,什么也没有拷过去
      

  3.   

    这个方法挺有创新啊。要是加上判断copy成功就好了。
      

  4.   

    copy 命令只能copy文件,不能copy文件夹吧?
    好相是这样。
      

  5.   

    private   int   dirCopy(string   dirFrom,string   dirTo)   
    {
          if(!Directory.Exists(dirFrom))   throw(new   Exception(dirFrom+"   not   exists"));   
          if(Directory.Exists(dirTo))   throw(new   Exception(dirTo+" exists"));   
          Directory.CreateDirectory(dirTo);   
          try{
                  foreach(string   f   in   Directory.GetFiles(dirFrom)){
                      File.Copy(f,dirTo+"\\"+Path.GetFileName(f));   
                  }
                  foreach(string   d   in   Directory.GetDirectories(dirFrom)){
                        dirCopy(d,dirTo+"\\"+Path.GetFileName(d));   
                  }   
                  return   0;
            }
            catch(Exception){   return   -1;}         

      

  6.   

    copy命令只能复制文件,
    xcopy命令是可以复制文件夹的
      

  7.   

    http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=208539
      

  8.   

    推荐使用API函数SHFileOperation,Windows本身就用这个操作文件,包括复制、移动、删除
      

  9.   

    viena(维也纳nn-实心木头人) (
    有已用过的c#代码吗?哪上面的一堆
    我拷贝下来一堆错误!
      

  10.   

    [StructLayout(LayoutKind.Sequential,    CharSet=CharSet.Auto,    Pack=1)] 
    [MarshalAs(UnmanagedType.U4)]    public    int    wFunc;   
    [MarshalAs(UnmanagedType.Bool)]    public    bool    fAnyOperationsAborted;  
    [DllImport("shell32.dll",    CharSet=CharSet.Auto)]  出错
    怎么回事?
      

  11.   

    记得Diagnostic Namespace 里面有地方可以用windows的prompt的,然后打xcopy之类的dos命令就可以了,记得path可能会是"\\"或者“/”,以前用过,手上没有VS,自己先试试吧
      

  12.   

    http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=208539
    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;      
                                 }      
                 }      
    }      
      

  13.   

    方法一:使用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);      
       
    }          
    }      
      

  14.   

    方法二:递归调用(程序简单,但比较慢)      
    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);      
                                                                         }      
                                                 }      
                         }      
    }
      

  15.   

    同意 viena(维也纳nn-实心木头人) 的
    用那个API即可。