文件夹复制操作(非递归循环遍历文件夹)
来源:互联网 发布:淘宝沙发推荐 编辑:程序博客网 时间:2024/05/16 14:37
/// 创建文件夹
/// </summary>
/// <param name="SourcePath">原始路径</param>
/// <returns></returns>
public static bool CreateFolder(string SourcePath)
{
try
{
Directory.CreateDirectory(SourcePath);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 复制文件夹[循环遍历]
/// </summary>
/// <param name="SourcePath">原始路径</param>
/// <param name="DestinPath">目地的路径</param>
/// <returns></returns>
public static bool CopyFolder(string SourcePath, string DestinPath)
{
if (Directory.Exists(SourcePath))
{
CreateFolder(DestinPath);//第一次创建跟目录文件夹
string sourcePath = SourcePath;//[变化的]原始路径
string destinPath = DestinPath;//[变化的]目地的路径
Queue<string> source = new Queue<string>();//存原始文件夹路径
Queue<string> destin = new Queue<string>();//存目地的文件夹路径
bool IsHasChildFolder = true;//是否有子文件夹
string tempDestinPath = string.Empty;//临时目地的,将被存于destin中
while (IsHasChildFolder)
{
string[] fileList = Directory.GetFileSystemEntries(sourcePath);// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
for (int i = 0; i < fileList.Length; i++)// 遍历所有的文件和目录
{
tempDestinPath = destinPath + "\\" + Path.GetFileName(fileList[i]);//取得子文件路径
if (Directory.Exists(fileList[i]))//存在文件夹时
{
source.Enqueue(fileList[i]);//当前的子目录的原始路径进队列
destin.Enqueue(tempDestinPath);//当前的子目录的目地的路径进队列
CreateFolder(tempDestinPath);//创建子文件夹
}
else//存在文件
{
File.Copy(fileList[i], tempDestinPath, true);//复制文件
}
}
if (source.Count > 0 && source.Count == destin.Count)//存在子文件夹时
{
sourcePath = source.Dequeue();
destinPath = destin.Dequeue();
}
else
{
IsHasChildFolder = false;
}
}
return true;
}
else
{
return false;
}
}
/// </summary>
/// <param name="SourcePath">原始路径</param>
/// <returns></returns>
public static bool CreateFolder(string SourcePath)
{
try
{
Directory.CreateDirectory(SourcePath);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 复制文件夹[循环遍历]
/// </summary>
/// <param name="SourcePath">原始路径</param>
/// <param name="DestinPath">目地的路径</param>
/// <returns></returns>
public static bool CopyFolder(string SourcePath, string DestinPath)
{
if (Directory.Exists(SourcePath))
{
CreateFolder(DestinPath);//第一次创建跟目录文件夹
string sourcePath = SourcePath;//[变化的]原始路径
string destinPath = DestinPath;//[变化的]目地的路径
Queue<string> source = new Queue<string>();//存原始文件夹路径
Queue<string> destin = new Queue<string>();//存目地的文件夹路径
bool IsHasChildFolder = true;//是否有子文件夹
string tempDestinPath = string.Empty;//临时目地的,将被存于destin中
while (IsHasChildFolder)
{
string[] fileList = Directory.GetFileSystemEntries(sourcePath);// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
for (int i = 0; i < fileList.Length; i++)// 遍历所有的文件和目录
{
tempDestinPath = destinPath + "\\" + Path.GetFileName(fileList[i]);//取得子文件路径
if (Directory.Exists(fileList[i]))//存在文件夹时
{
source.Enqueue(fileList[i]);//当前的子目录的原始路径进队列
destin.Enqueue(tempDestinPath);//当前的子目录的目地的路径进队列
CreateFolder(tempDestinPath);//创建子文件夹
}
else//存在文件
{
File.Copy(fileList[i], tempDestinPath, true);//复制文件
}
}
if (source.Count > 0 && source.Count == destin.Count)//存在子文件夹时
{
sourcePath = source.Dequeue();
destinPath = destin.Dequeue();
}
else
{
IsHasChildFolder = false;
}
}
return true;
}
else
{
return false;
}
}
0 0
- 文件夹复制操作(非递归循环遍历文件夹) C#
- 文件夹复制操作(非递归循环遍历文件夹)
- 非递归遍历文件夹
- 非递归遍历文件夹
- 非递归遍历文件夹
- 非递归遍历文件夹
- java递归遍历复制文件夹
- java遍历文件夹(非递归)
- 非递归遍历指定文件夹
- 递归遍历文件夹复制指定后缀文件
- 非递归方式遍历ftp文件夹
- 非递归方式遍历本地文件夹
- 文件夹遍历的非递归实现
- 遍历文件夹以及复制文件夹
- java 遍历递归操作(改名) 文件/文件夹
- 文件夹遍历递归与非递归算法(C#实现)
- 递归复制文件夹
- C#递归复制文件夹
- GridView批量删除页面最后N条数据时自动翻到上一页问题
- Cookie问题(烦了三天)
- asp.net 2.0 简单实现url重写
- 应用Url重写时CSS引用问题
- asp.net 2.0中设定默认焦点按钮
- 文件夹复制操作(非递归循环遍历文件夹)
- Asp.net基础阶段化分
- 利用WeX5平台实现单图片上传功能
- xml 序列化操作
- 算法题:水杯倒水的问题
- msql表里的数据的简单复制并且修改
- 软件2班36人[扑林作]
- 通过JDBC访问hive集群
- 计算器之如何从字符串表达式中有效提取数字