开发过程中很多时候我们生成的数据存在文件中
但是我们有需要把文件夹中的文件或者文件夹移动到其它盘作为备份
现在整理了一个例子关于C#通过指定电脑磁盘路径里的文件移动到别的盘指定路径的代码
我们可以先定义一个静态的方法CopyFolder() 让它带两个参数string sourcePath, string destPath
sourcePath为源文件
destPath为目标文件
方法代码实现如下:
if (Directory.Exists(sourcePath))
{
if (!Directory.Exists(destPath))
{
//目标目录不存在则创建
try
{
Directory.CreateDirectory(destPath);
}
catch (Exception ex)
{
throw new Exception("创建目标目录失败:" + ex.Message);
}
}
//获得源文件下所有文件
List<string> files = new List<string>(Directory.GetFiles(sourcePath));
files.ForEach(c =>
{