CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

如何复制文件夹?

楼主lxg2000(失落的神殿)2005-07-11 16:37:20 在 .NET技术 / C# 提问

c#有没有能实现类似dos的xcopy功能的函数  
  文件夹和文件夹下的文件拷贝的功能。  
  问题点数:100、回复次数:7Top

1 楼JzeroBiao(先知)回复于 2005-07-11 16:40:16 得分 10

System.IO.DirectoryTop

2 楼weixinzhu(我想哭,可是我没有眼泪)回复于 2005-07-11 16:44:49 得分 10

System.IO.DirectoryinfoTop

3 楼lxg2000(失落的神殿)回复于 2005-07-11 16:45:53 得分 0

具体函数呢?Top

4 楼lovefootball(蟑螂(生活就是扯淡--做人要放低姿态))回复于 2005-07-11 16:58:43 得分 10

System.IO.Directory.Move(sourcePath,   destinationPath);  
   
  Top

5 楼qpl007(蓝色闪电)回复于 2005-07-11 17:09:01 得分 10

System.IO.Directory.Move(sourcePath,   destinationPath);Top

6 楼builder666666(nj)回复于 2005-07-11 17:14:53 得分 10

System.IO.Directory.Move(oldPath,   newPath);  
  Top

7 楼hawk5456(雨人)回复于 2005-07-12 14:27:14 得分 50

以前发过的给你看看啦。  
  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);      
                                                                        }      
                                                }      
                        }      
  }      
     
  Top

相关问题

  • 如何复制文件夹
  • 在delphi5中如何复制文件夹?
  • 如何复制文件和文件夹?
  • 请问 如何复制文件夹以及文件夹中的全部内容?
  • 复制文件夹
  • 复制文件夹!
  • 如何写复制整个文件夹的程序?
  • asp如何实现复制一个文件夹
  • 在C#中如何实现文件夹的复制????
  • 如何将从一个文件夹中复制一个文件到另外一个文件夹中?

关键词

  • c#
  • 文件夹
  • fsi
  • destname
  • directoryinfo
  • destdir
  • srcdir
  • recursive
  • directory
  • environment

得分解答快速导航

  • 帖主:lxg2000
  • JzeroBiao
  • weixinzhu
  • lovefootball
  • qpl007
  • builder666666
  • hawk5456

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo