using System;
using System.Text;
using System.IO;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
string DirName = @"d:\";//路径名
string FileName = ".jpg"; //文件中包含名
string CurrentDir = AppDomain.CurrentDomain.BaseDirectory;//获取程序所在位置
string ReslultPath = CurrentDir + @"\Result.txt";
File.AppendAllText(ReslultPath,"\t\t\t\t"+"扫描时间:"+ DateTime.Now.ToString()+ "\r\n");
GetFileName(DirName, FileName,ReslultPath);
Console.WriteLine("Success!");
Console.ReadLine();
}
static void GetFileName(string DirName, string FileName, string ReslultPath)
{
DirectoryInfo dir = new DirectoryInfo(DirName);//文件夹信息
if (null != dir.Parent && dir.Attributes.ToString().IndexOf("System") > -1)//如果非根路径且是系统文件夹则跳过
{
return;
}
FileInfo[] fileinfo = dir.GetFiles(); //取得所有文件
string filename = string.Empty;
for (int i = 0; i < fileinfo.Length; i++)
{
filename = fileinfo[i].Name;//判断文件是否包含查询名
if (filename.IndexOf(FileName) > -1)
{
Console.WriteLine(fileinfo[i].FullName);
File.AppendAllText(ReslultPath, fileinfo[i].FullName + "\r\n");//将结果写入到文件
}
}
DirectoryInfo[] dinfo = dir.GetDirectories();//取得所有子文件夹
for (int i = 0; i < dinfo.Length; i++)
{
GetFileName(dinfo[i].FullName, FileName, ReslultPath);//查找子文件夹中是否有符合要求的文件
}
}
}
}