拷贝文件夹的一个函数

public static string CopyDirectory(string source, string destination, bool IsBaseFolder)
        {
            try
            {
                DirectoryInfo sourceInfo = new DirectoryInfo(source);
                DirectoryInfo destinationInfo = new DirectoryInfo(destination);

                if (IsBaseFolder)
                {
                    destinationInfo.CreateSubdirectory(sourceInfo.Name);
                    CopyDirectory(source, Path.Combine(destination, sourceInfo.Name), false);
                }
                else
                {
                    foreach (FileInfo file in sourceInfo.GetFiles())
                    {
                        file.CopyTo(destination + @"\" + file.Name, true);
                    }

                    foreach (DirectoryInfo subDirectory in sourceInfo.GetDirectories())
                    {
                        destinationInfo.CreateSubdirectory(subDirectory.Name);
                        CopyDirectory(Path.Combine(source, subDirectory.Name), Path.Combine(destination, subDirectory.Name), false);
                    }
                }

                return Path.Combine(destination, sourceInfo.Name);
            }
            catch (Exception exp)
            {
                Console.WriteLine("Unable to copy to shared path: {0}.\r\n", destination);
                Console.WriteLine("Detailed error is as below:", exp.InnerException);
                return string.Empty;
            }
        }

你可能感兴趣的:(文件夹)