水晴云朗

博客园 首页 新随笔 联系 订阅 管理
Windows在管理目录和文件时存在着区别,我们不能将目录结构和文件结构混淆。因为Win32API就没有提供类似的功能,所以在.NET中也就没有提供相应的封装。其实我们只要自己实现该Copy方法即可,实现的思路就是遍历该目录中的所有文件,并把文件拷贝到目标目录中。以下是部分示例代码
  1class CopyFolder
  2
  3    {
  4
  5        /// <summary>
  6
  7        /// The main entry point for the application.
  8
  9        /// </summary>

 10
 11        [STAThread]
 12
 13        static void Main(string[] args)
 14
 15        {
 16
 17            //
 18
 19            // TODO: Add code to start application here
 20
 21            //
 22
 23            DirectoryInfo source = new DirectoryInfo("D:\\My Folder");
 24
 25            DirectoryInfo destination = new DirectoryInfo("E:\\My Folder");
 26
 27            Copy(source, destination, nullnulltrue);
 28
 29        }

 30
 31 
 32
 33        /// <SUMMARY>
 34
 35        /// Copy a Directory, SubDirectories and Files Given a Source and  
 36
 37        /// Destination DirectoryInfo Object, Given a SubDirectory Filter
 38
 39        /// and a File Filter.
 40
 41        /// IMPORTANT: The search strings for SubDirectories and Files applies 
 42
 43        /// to every Folder and File within the Source Directory.
 44
 45        /// </SUMMARY>
 46
 47        /// <PARAM name="SourceDirectory">A DirectoryInfo Object Pointing 
 48
 49        /// to the Source Directory</PARAM>
 50
 51        /// <PARAM name="DestinationDirectory">A DirectoryInfo Object Pointing 
 52
 53        /// to the Destination Directory</PARAM>
 54
 55        /// <PARAM name="SourceDirectoryFilter">Search String on  
 56
 57        ///   SubDirectories (Example: "System*" will return all subdirectories
 58
 59        ///   starting with "System")</PARAM>
 60
 61        /// <PARAM name="SourceFileFilter">File Filter: Standard DOS-Style Format 
 62
 63        ///    (Examples: "*.txt" or "*.exe")</PARAM>
 64
 65        /// <PARAM name="Overwrite">Whether or not to Overwrite Copied Files in the
 66
 67        ///     Destination Directory</PARAM>

 68
 69        public static void Copy(DirectoryInfo SourceDirectory,
 70
 71            DirectoryInfo DestinationDirectory, string SourceDirectoryFilter, 
 72
 73            string SourceFileFilter, bool Overwrite)
 74
 75        {
 76
 77            DirectoryInfo[] SourceSubDirectories;
 78
 79            FileInfo[] SourceFiles;
 80
 81 
 82
 83            //Check for File Filter
 84
 85            if (SourceFileFilter != null)
 86
 87                SourceFiles = SourceDirectory.GetFiles(SourceFileFilter.Trim());
 88
 89            else
 90
 91                SourceFiles = SourceDirectory.GetFiles();
 92
 93 
 94
 95            //Check for Folder Filter
 96
 97            if (SourceDirectoryFilter != null)
 98
 99                SourceSubDirectories = SourceDirectory.GetDirectories(
100
101                    SourceDirectoryFilter.Trim());
102
103            else
104
105                SourceSubDirectories = SourceDirectory.GetDirectories();
106
107 
108
109            //Create the Destination Directory
110
111            if (!DestinationDirectory.Exists) DestinationDirectory.Create();
112
113            
114
115            //Recursively Copy Every SubDirectory and it's 
116
117            //Contents (according to folder filter)
118
119            foreach (DirectoryInfo SourceSubDirectory in SourceSubDirectories)
120
121                Copy(SourceSubDirectory, new DirectoryInfo(
122
123                    DestinationDirectory.FullName + @"\" + SourceSubDirectory.Name), 
124
125                    SourceDirectoryFilter, SourceFileFilter, Overwrite);
126
127 
128
129            //Copy Every File to Destination Directory (according to file filter)
130
131            foreach (FileInfo SourceFile in SourceFiles)
132
133                SourceFile.CopyTo(DestinationDirectory.FullName + 
134
135                    @"\" + SourceFile.Name, Overwrite);
136
137        }

138
139    }

140
posted on 2005-08-18 10:46  遠神惠賜  阅读(270)  评论(0)    收藏  举报