using System; |
using System.Collections.Generic; |
using System.Text; |
using System.IO; |
namespace ConsoleApplication1 |
{ |
class Program |
{ |
static void Main ( string [] args ) |
{ |
//实例化test类,用于调用该类中的copy函数 |
test a = new test(); |
a.copy ( "D:\\新建文件夹(2)" , "D:\\新建文件夹" ); //试验 |
} |
//test类的编写 |
public sealed class test |
{ |
public test() |
{ |
} |
public void copy ( string source, string destination ) //source 是原文件夹的路径,destination是要复制到的目标文件夹路径 |
{ |
//调用文件夹复制函数 |
test.CopyDirectory ( source, destination ); |
//将原文件夹里的内容写进目标文件夹下的"记录.txt"里 |
StreamWriter writer = new StreamWriter ( destination + "\\记录.txt" ); |
//Directory.GetFileSystemEntries() 可以得到指定目录包含子文件夹以及文件名字的字符串数组 |
string [] names = Directory.GetFileSystemEntries ( source ); |
//历遍names数组,把原文件那些名字全写进去 |
foreach ( string name in names ) |
{ |
writer.WriteLine ( name ); |
} |
writer.Close(); |
} |
//文件夹复制函数编写 |
public static void CopyDirectory ( String source, String destination ) |
{ |
DirectoryInfo info = new DirectoryInfo ( source ); |
foreach ( FileSystemInfo fsi in info.GetFileSystemInfos() ) |
{ |
//目标路径destName = 目标文件夹路径 + 原文件夹下的子文件(或文件夹)名字 |
//Path.Combine(string a ,string b) 为合并两个字符串 |
String destName = Path.Combine ( destination, fsi.Name ); |
//如果是文件类,就复制文件 |
if ( fsi is System.IO.FileInfo ) |
File.Copy ( fsi.FullName, destName ); |
//如果不是 则为文件夹,继续调用文件夹复制函数,递归 |
else |
{ |
Directory.CreateDirectory ( destName ); |
CopyDirectory ( fsi.FullName, destName ); |
} |
} |
} |
} |
//CopyDirectory 这个函数是我引用别人的,写的很好就留下了,我只是在外面加了个把信息记录下来而已 |
} |
} |
by: 发表于:2018-01-15 09:54:56 顶(0) | 踩(0) 回复
??
回复评论