c#怎么搜索指定目录及其所有子文件夹的exe文件然后列表显示并复制到另一个目录。。。求代码
1个回答
展开全部
static void CopyAllFiles(string fpath, string tpath, string filetype)
{
if (Directory.Exists(fpath))
{
foreach (string f in Directory.GetFiles(fpath, "*." + filetype))
{
string filename = Path.GetFileName(f);
Console.WriteLine("{0},{1}", f, filename);
File.Copy(f, Path.Combine(tpath, filename), true);
}
foreach (string f in Directory.GetDirectories(fpath))
{
CopyAllFiles(f, tpath, filetype);
}
}
}
static void Main(string[] args)
{
Console.WriteLine("请输入搜索目录:");
string searchFolder = Console.ReadLine();
Console.WriteLine("请输入复制的目标目录:");
string toFolder = Console.ReadLine();
string fileType = "exe"
CopyAllFiles(searchFolder, toFolder, fileType);
Console.Read();
}
{
if (Directory.Exists(fpath))
{
foreach (string f in Directory.GetFiles(fpath, "*." + filetype))
{
string filename = Path.GetFileName(f);
Console.WriteLine("{0},{1}", f, filename);
File.Copy(f, Path.Combine(tpath, filename), true);
}
foreach (string f in Directory.GetDirectories(fpath))
{
CopyAllFiles(f, tpath, filetype);
}
}
}
static void Main(string[] args)
{
Console.WriteLine("请输入搜索目录:");
string searchFolder = Console.ReadLine();
Console.WriteLine("请输入复制的目标目录:");
string toFolder = Console.ReadLine();
string fileType = "exe"
CopyAllFiles(searchFolder, toFolder, fileType);
Console.Read();
}
追问
能不能尽量多加入点注释呢?谢谢
追答
思想就是用递归循环读取目录下的所有文件
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询