[C#] 复制文件夹 →→→→→进入此内容的聊天室

来自 , 2019-06-30, 写在 C#, 查看 97 次.
URL http://www.code666.cn/view/1b113258
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. namespace ConsoleApplication1
  6. {
  7. class Program
  8. {
  9.         static void Main ( string[] args )
  10.         {
  11.                 //实例化test类,用于调用该类中的copy函数
  12.                 test a = new test();
  13.                 a.copy ( "D:\\新建文件夹(2)", "D:\\新建文件夹" );//试验
  14.         }
  15.         //test类的编写
  16.         public sealed class test
  17.         {
  18.                 public test()
  19.                 {
  20.                 }
  21.                 public void copy ( string source, string destination ) //source 是原文件夹的路径,destination是要复制到的目标文件夹路径
  22.                 {
  23.                         //调用文件夹复制函数
  24.                         test.CopyDirectory ( source, destination );
  25.                         //将原文件夹里的内容写进目标文件夹下的"记录.txt"里
  26.                         StreamWriter writer = new StreamWriter ( destination + "\\记录.txt" );
  27.                         //Directory.GetFileSystemEntries() 可以得到指定目录包含子文件夹以及文件名字的字符串数组
  28.                         string[] names = Directory.GetFileSystemEntries ( source );
  29.                         //历遍names数组,把原文件那些名字全写进去
  30.                         foreach ( string name in names )
  31.                         {
  32.                                 writer.WriteLine ( name );
  33.                         }
  34.                         writer.Close();
  35.                 }
  36.                 //文件夹复制函数编写
  37.                 public static void CopyDirectory ( String source, String destination )
  38.                 {
  39.                         DirectoryInfo info = new DirectoryInfo ( source );
  40.                         foreach ( FileSystemInfo fsi in info.GetFileSystemInfos() )
  41.                         {
  42.                                 //目标路径destName = 目标文件夹路径 + 原文件夹下的子文件(或文件夹)名字
  43.                                 //Path.Combine(string a ,string b) 为合并两个字符串
  44.                                 String destName = Path.Combine ( destination, fsi.Name );
  45.                                 //如果是文件类,就复制文件
  46.                                 if ( fsi is System.IO.FileInfo )
  47.                                         File.Copy ( fsi.FullName, destName );
  48.                                 //如果不是 则为文件夹,继续调用文件夹复制函数,递归
  49.                                 else
  50.                                 {
  51.                                         Directory.CreateDirectory ( destName );
  52.                                         CopyDirectory ( fsi.FullName, destName );
  53.                                 }
  54.                         }
  55.                 }
  56.         }
  57.         //CopyDirectory 这个函数是我引用别人的,写的很好就留下了,我只是在外面加了个把信息记录下来而已
  58. }
  59. }

回复 "复制文件夹"

这儿你可以回复上面这条便签

captcha