using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace 避免回溯方法
{
class Program
{
static void Main(string[] args)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
List<string> fileList1 = new List<string>();
FunctionHuishuo(path, ref fileList1);
List<string> fileList2 = new List<string>();
FunctionQueue(path, ref fileList2);
List<string> fileList3 = new List<string>();
FunctionStack(path, ref fileList3);
}
/// <summary>
/// 回溯法
/// </summary>
/// <param name="path"></param>
/// <param name="fileList"></param>
private static void FunctionHuishuo(string path, ref List<string> fileList)
{
if (true)
{
string[] files = null;
try
{
files = Directory.GetFiles(path);
}
catch { }
if (files != null && files.Length > 0)
{
fileList.AddRange(files);
}
}
if (true)
{
string[] folders = null;
try
{
folders = Directory.GetDirectories(path);
}
catch { }
if (folders != null && folders.Length > 0)
{
foreach (string folder in folders)
{
FunctionHuishuo(folder, ref fileList);
}
}
}
}
/// <summary>
/// 堆栈法
/// </summary>
/// <param name="path"></param>
private static void FunctionStack(string path, ref List<string> fileList)
{
Stack<string> stack = new Stack<string>();
stack.Push(path);
while (stack.Count > 0)
{
string dir = stack.Pop();
string[] files = null;
try
{
files = Directory.GetFiles(dir);
}
catch { }
if (files != null && files.Length > 0)
{
fileList.AddRange(files);
}
string[] folders = null;
try
{
folders = Directory.GetDirectories(dir);
}
catch { }
if (folders != null && folders.Length > 0)
{
foreach (string folder in folders)
{
stack.Push(folder);
}
}
}
}
/// <summary>
/// 队列法
/// </summary>
/// <param name="path"></param>
private static void FunctionQueue(string path, ref List<string> fileList)
{
Queue<string> queue = new Queue<string>();
queue.Enqueue(path);
while (queue.Count > 0)
{
string dir = queue.Dequeue();
string[] files = null;
try
{
files = Directory.GetFiles(dir);
}
catch { }
if (files != null && files.Length > 0)
{
fileList.AddRange(files);
}
string[] folders = null;
try
{
folders = Directory.GetDirectories(dir);
}
catch { }
if (folders != null && folders.Length > 0)
{
foreach (string folder in folders)
{
queue.Enqueue(folder);
}
}
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有